diff --git a/default.go b/default.go index 0e9364a..2ff74a2 100644 --- a/default.go +++ b/default.go @@ -9,6 +9,14 @@ import ( type DefaultError uint32 +type DefaultErrorHandler struct { + DefaultError +} + +func (*DefaultErrorHandler) ErrorResponse(errorResponse ErrorResponse) ErrorResponse { + return DefaultErrorHandler{errorResponse.(DefaultError)} +} + const ( DefaultOk DefaultError = http.StatusOK DefaultErrorNotImplemented DefaultError = http.StatusNotImplemented @@ -58,6 +66,7 @@ func (e DefaultError) BodyBytes() (body []byte) { type DefaultRequestBuilder struct { requestBuilder RequestBuilder prototype PrototypeRequestBuilder + errorHandler ErrorHandler allowedMethods *[]string header struct { *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 { amd := make([]string, 0) am = &amd @@ -99,6 +111,7 @@ func NewDefaultRequestBuilder(rb RequestBuilder, rbp PrototypeRequestBuilder, am return DefaultRequestBuilder{ requestBuilder: rb, prototype: rbp, + errorHandler: eh, allowedMethods: am, header: struct { *Header @@ -136,7 +149,7 @@ func (rb *DefaultRequestBuilder) Allowed(method string) (errRes ErrorResponse) { } } if !ok { - return DefaultErrorMethodNotAllowed + return rb.errorHandler.ErrorResponse(DefaultErrorMethodNotAllowed) } return DefaultOk } @@ -144,23 +157,23 @@ func (rb *DefaultRequestBuilder) Allowed(method string) (errRes ErrorResponse) { func (rb *DefaultRequestBuilder) Header(header Header) (errRes ErrorResponse) { err := rb.header.Header.Parse(header) 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) { defer body.Close() json.NewDecoder(body).Decode(rb.body.fields) - return DefaultOk + return rb.errorHandler.ErrorResponse(DefaultOk) } func (rb *DefaultRequestBuilder) Values(values Values) (errRes ErrorResponse) { err := rb.values.Values.Parse(values) if err != nil { - return DefaultErrorBadRequest + return rb.errorHandler.ErrorResponse(DefaultErrorBadRequest) } - return DefaultOk + return rb.errorHandler.ErrorResponse(DefaultOk) } type DefaultResponse struct { diff --git a/prototype.go b/prototype.go index 3692ce3..9cdc559 100644 --- a/prototype.go +++ b/prototype.go @@ -32,6 +32,11 @@ type ErrorResponse interface { Error() (out string) } +type ErrorHandler interface { + ErrorResponse + ErrorResponse(errorResponse ErrorResponse) ErrorResponse +} + type Prototype struct { PrototypeRequestBuilder PrototypeResponse