From 64f15565fbc279a03e91e48004b501199d5c00f9 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Tue, 30 Jul 2024 11:51:55 -0300 Subject: [PATCH] fix(errors): errors handling interface implementation --- internals/router/errors/400.go | 28 +++++++++++++++++++++++++--- internals/router/errors/500.go | 9 +++++---- internals/router/errors/errors.templ | 21 +++++++++------------ 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/internals/router/errors/400.go b/internals/router/errors/400.go index 9ecd8d0..b8e8557 100644 --- a/internals/router/errors/400.go +++ b/internals/router/errors/400.go @@ -6,15 +6,37 @@ import ( "strings" ) +type ErrBadRequest struct { + Msg string `json:"message"` +} + +func NewErrBadRequest(format string, a ...string) RouteErrorHandler { + return defaultErrorHandler{ErrBadRequest{Msg: fmt.Sprintf(format, a)}} +} +func (e ErrBadRequest) Error() string { return e.Msg } +func (e ErrBadRequest) Status() int { return http.StatusBadRequest } + type ErrMissingParams struct { - defaultErr Params []string `json:"params"` } -func NewErrMissingParams(params ...string) ErrMissingParams { - return ErrMissingParams{Params: params} +func NewErrMissingParams(params ...string) RouteErrorHandler { + return defaultErrorHandler{ErrMissingParams{Params: params}} } func (e ErrMissingParams) Error() string { return fmt.Sprintf("Missing parameters: %s.", strings.Join(e.Params, ", ")) } func (e ErrMissingParams) Status() int { return http.StatusBadRequest } + +type ErrMethodNotAllowed struct { + Method string `json:"method"` + Allowed []string `json:"allowed"` +} + +func NewErrMethodNotAllowed(method string, allowed ...string) RouteErrorHandler { + return defaultErrorHandler{ErrMethodNotAllowed{Method: method, Allowed: allowed}} +} +func (e ErrMethodNotAllowed) Error() string { + return fmt.Sprintf("Method %s not allowed. Allowed methods are: %s", e.Method, strings.Join(e.Allowed, ", ")) +} +func (e ErrMethodNotAllowed) Status() int { return http.StatusMethodNotAllowed } diff --git a/internals/router/errors/500.go b/internals/router/errors/500.go index 233e916..70b337e 100644 --- a/internals/router/errors/500.go +++ b/internals/router/errors/500.go @@ -6,10 +6,11 @@ import ( ) type ErrInternal struct { - defaultErr Err string `json:"error"` } -func NewErrInternal(err ...error) ErrInternal { return ErrInternal{Err: errors.Join(err...).Error()} } -func (e ErrInternal) Error() string { return e.Err } -func (e ErrInternal) Status() int { return http.StatusInternalServerError } +func NewErrInternal(err ...error) RouteErrorHandler { + return defaultErrorHandler{ErrInternal{Err: errors.Join(err...).Error()}} +} +func (e ErrInternal) Error() string { return e.Err } +func (e ErrInternal) Status() int { return http.StatusInternalServerError } diff --git a/internals/router/errors/errors.templ b/internals/router/errors/errors.templ index 530db45..4ed734d 100644 --- a/internals/router/errors/errors.templ +++ b/internals/router/errors/errors.templ @@ -7,25 +7,22 @@ import ( "encoding/json" ) -type Error interface { +type RouteError interface { Error() string Status() int +} + +type RouteErrorHandler interface { ServeHTTP(w http.ResponseWriter, r *http.Request) Component() templ.Component JSON() string } -type defaultErr struct{} - -func (e defaultErr) Error() string { - return "Error: This method should have been overridden :')" +type defaultErrorHandler struct { + RouteError } -func (e defaultErr) Status() int { - return http.StatusNotImplemented -} - -func (e defaultErr) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (e defaultErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.Header().Set("Content-Type", "text/html") @@ -50,7 +47,7 @@ func (e defaultErr) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(e.Status()) } -func (e defaultErr) JSON() string { +func (e defaultErrorHandler) JSON() string { type jsonErr struct { Error string `json:"error"` Info any `json:"info"` @@ -69,7 +66,7 @@ func (e defaultErr) JSON() string { return string(js) } -templ (e defaultErr) Component() { +templ (e defaultErrorHandler) Component() { @layouts.Page("Error") {