Allow own ErrorHandler for Defaults

This commit is contained in:
some 2024-10-09 01:01:14 -04:00
parent 0165e58ddb
commit 64fe69244c
Signed by: some
GPG Key ID: 65D0589220B9BFC8
2 changed files with 25 additions and 7 deletions

View File

@ -9,6 +9,14 @@ import (
type DefaultError uint32 type DefaultError uint32
type DefaultErrorHandler struct {
DefaultError
}
func (*DefaultErrorHandler) ErrorResponse(errorResponse ErrorResponse) ErrorResponse {
return DefaultErrorHandler{errorResponse.(DefaultError)}
}
const ( const (
DefaultOk DefaultError = http.StatusOK DefaultOk DefaultError = http.StatusOK
DefaultErrorNotImplemented DefaultError = http.StatusNotImplemented DefaultErrorNotImplemented DefaultError = http.StatusNotImplemented
@ -58,6 +66,7 @@ func (e DefaultError) BodyBytes() (body []byte) {
type DefaultRequestBuilder struct { type DefaultRequestBuilder struct {
requestBuilder RequestBuilder requestBuilder RequestBuilder
prototype PrototypeRequestBuilder prototype PrototypeRequestBuilder
errorHandler ErrorHandler
allowedMethods *[]string allowedMethods *[]string
header struct { header struct {
*Header *Header
@ -73,7 +82,10 @@ type DefaultRequestBuilder struct {
} }
} }
func NewDefaultRequestBuilder(rb RequestBuilder, rbp PrototypeRequestBuilder, am *[]string, h *Header, hf header, v *Values, vf values, b *Body, bf body) DefaultRequestBuilder { func NewDefaultRequestBuilder(rb RequestBuilder, rbp PrototypeRequestBuilder, eh ErrorHandler, am *[]string, h *Header, hf header, v *Values, vf values, b *Body, bf body) DefaultRequestBuilder {
if eh == nil {
eh = &DefaultErrorHandler{}
}
if am == nil { if am == nil {
amd := make([]string, 0) amd := make([]string, 0)
am = &amd am = &amd
@ -99,6 +111,7 @@ func NewDefaultRequestBuilder(rb RequestBuilder, rbp PrototypeRequestBuilder, am
return DefaultRequestBuilder{ return DefaultRequestBuilder{
requestBuilder: rb, requestBuilder: rb,
prototype: rbp, prototype: rbp,
errorHandler: eh,
allowedMethods: am, allowedMethods: am,
header: struct { header: struct {
*Header *Header
@ -136,7 +149,7 @@ func (rb *DefaultRequestBuilder) Allowed(method string) (errRes ErrorResponse) {
} }
} }
if !ok { if !ok {
return DefaultErrorMethodNotAllowed return rb.errorHandler.ErrorResponse(DefaultErrorMethodNotAllowed)
} }
return DefaultOk return DefaultOk
} }
@ -144,23 +157,23 @@ func (rb *DefaultRequestBuilder) Allowed(method string) (errRes ErrorResponse) {
func (rb *DefaultRequestBuilder) Header(header Header) (errRes ErrorResponse) { func (rb *DefaultRequestBuilder) Header(header Header) (errRes ErrorResponse) {
err := rb.header.Header.Parse(header) err := rb.header.Header.Parse(header)
if err != nil { if err != nil {
return DefaultErrorBadRequest return rb.errorHandler.ErrorResponse(DefaultErrorBadRequest)
} }
return DefaultOk return rb.errorHandler.ErrorResponse(DefaultOk)
} }
func (rb *DefaultRequestBuilder) ReadBody(body io.ReadCloser) (errRes ErrorResponse) { func (rb *DefaultRequestBuilder) ReadBody(body io.ReadCloser) (errRes ErrorResponse) {
defer body.Close() defer body.Close()
json.NewDecoder(body).Decode(rb.body.fields) json.NewDecoder(body).Decode(rb.body.fields)
return DefaultOk return rb.errorHandler.ErrorResponse(DefaultOk)
} }
func (rb *DefaultRequestBuilder) Values(values Values) (errRes ErrorResponse) { func (rb *DefaultRequestBuilder) Values(values Values) (errRes ErrorResponse) {
err := rb.values.Values.Parse(values) err := rb.values.Values.Parse(values)
if err != nil { if err != nil {
return DefaultErrorBadRequest return rb.errorHandler.ErrorResponse(DefaultErrorBadRequest)
} }
return DefaultOk return rb.errorHandler.ErrorResponse(DefaultOk)
} }
type DefaultResponse struct { type DefaultResponse struct {

View File

@ -32,6 +32,11 @@ type ErrorResponse interface {
Error() (out string) Error() (out string)
} }
type ErrorHandler interface {
ErrorResponse
ErrorResponse(errorResponse ErrorResponse) ErrorResponse
}
type Prototype struct { type Prototype struct {
PrototypeRequestBuilder PrototypeRequestBuilder
PrototypeResponse PrototypeResponse