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 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 {

View File

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