From a2b6a95085ef7a399ea0af9a429b8a34694dcd8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20=C3=81lvarez?= <86166683+ralvarezdev@users.noreply.github.com> Date: Sun, 2 Feb 2025 19:13:19 -0400 Subject: [PATCH] refactor: renamed RequestError as FailRequestError * Renamed RequestError as FailRequestError * Now, CookieError implements the FailRequestError interface --- http/handler/handler.go | 6 ++--- http/middleware/auth/authenticator.go | 2 +- http/middleware/auth/middleware.go | 20 +++++++------- ...request_error.go => fail_request_error.go} | 27 ++++++++++++------- 4 files changed, 31 insertions(+), 24 deletions(-) rename http/response/{request_error.go => fail_request_error.go} (81%) diff --git a/http/handler/handler.go b/http/handler/handler.go index ade82b2..3061ed1 100644 --- a/http/handler/handler.go +++ b/http/handler/handler.go @@ -234,11 +234,11 @@ func (d *DefaultHandler) HandleError( w http.ResponseWriter, err error, ) { - // Check if the errors is a request error - var e gonethttpresponse.RequestError + // Check if the errors is a fail request error + var e gonethttpresponse.FailRequestError if errors.As(err, &e) { d.HandleResponse( - w, gonethttpresponse.NewResponseFromRequestError(e), + w, gonethttpresponse.NewResponseFromFailRequestError(e), ) return } diff --git a/http/middleware/auth/authenticator.go b/http/middleware/auth/authenticator.go index efaa777..2e6c7af 100644 --- a/http/middleware/auth/authenticator.go +++ b/http/middleware/auth/authenticator.go @@ -8,7 +8,7 @@ import ( // Authenticator interface type Authenticator interface { Authenticate( - errorHandler func( + failHandler func( w http.ResponseWriter, err string, httpStatus int, diff --git a/http/middleware/auth/middleware.go b/http/middleware/auth/middleware.go index 8660d00..8af6997 100644 --- a/http/middleware/auth/middleware.go +++ b/http/middleware/auth/middleware.go @@ -40,7 +40,7 @@ func NewMiddleware( // Authenticate return the middleware function that authenticates the request func (m *Middleware) Authenticate( - errorHandler func( + failHandler func( w http.ResponseWriter, err string, httpStatus int, @@ -58,7 +58,7 @@ func (m *Middleware) Authenticate( token, ) if err != nil { - errorHandler( + failHandler( w, err.Error(), http.StatusUnauthorized, @@ -81,8 +81,8 @@ func (m *Middleware) Authenticate( func (m *Middleware) AuthenticateFromHeader( token gojwttoken.Token, ) func(next http.Handler) http.Handler { - // Create the error handler function - errorHandler := func( + // Create the fail handler function + failHandler := func( w http.ResponseWriter, err string, httpStatus int, @@ -110,7 +110,7 @@ func (m *Middleware) AuthenticateFromHeader( // Return an error if the authorization is missing or invalid if len(parts) < 2 || parts[0] != gojwt.BearerPrefix { - errorHandler( + failHandler( w, ErrInvalidAuthorizationHeader.Error(), http.StatusUnauthorized, @@ -123,7 +123,7 @@ func (m *Middleware) AuthenticateFromHeader( rawToken := parts[1] // Call the Authenticate function - m.Authenticate(errorHandler, token, rawToken)(next).ServeHTTP( + m.Authenticate(failHandler, token, rawToken)(next).ServeHTTP( w, r, ) @@ -137,8 +137,8 @@ func (m *Middleware) AuthenticateFromCookie( token gojwttoken.Token, cookieName string, ) func(next http.Handler) http.Handler { - // Create the error handler function - errorHandler := func( + // Create the fail handler function + failHandler := func( w http.ResponseWriter, err string, httpStatus int, @@ -163,7 +163,7 @@ func (m *Middleware) AuthenticateFromCookie( // Return an error if the cookie is missing if err != nil { - errorHandler( + failHandler( w, gonethttp.ErrCookieNotFound.Error(), http.StatusUnauthorized, @@ -176,7 +176,7 @@ func (m *Middleware) AuthenticateFromCookie( rawToken := cookie.Value // Call the Authenticate function - m.Authenticate(errorHandler, token, rawToken)(next).ServeHTTP( + m.Authenticate(failHandler, token, rawToken)(next).ServeHTTP( w, r, ) diff --git a/http/response/request_error.go b/http/response/fail_request_error.go similarity index 81% rename from http/response/request_error.go rename to http/response/fail_request_error.go index 48cc39e..062c6a3 100644 --- a/http/response/request_error.go +++ b/http/response/fail_request_error.go @@ -1,8 +1,8 @@ package response type ( - // RequestError struct - RequestError interface { + // FailRequestError struct + FailRequestError interface { Key() string Error() string HTTPStatus() int @@ -115,6 +115,11 @@ func (c *CookieError) Name() string { return c.name } +// Key returns the cookie name +func (c *CookieError) Key() string { + return c.Name() +} + // Error returns the cookie error as a string func (c *CookieError) Error() string { return c.err @@ -130,25 +135,27 @@ func (c *CookieError) ErrorCode() *string { return c.errorCode } -// NewRequestErrorBodyData creates a new request errors body data -func NewRequestErrorBodyData( - requestError RequestError, +// NewRequestErrorsBodyData creates a new request errors body data +func NewRequestErrorsBodyData( + requestErrors ...FailRequestError, ) *map[string]*[]string { // Initialize the request errors map requestErrorsMap := make(map[string]*[]string) // Add the request error to the request errors map - requestErrorsMap[requestError.Key()] = &[]string{requestError.Error()} + for _, requestError := range requestErrors { + requestErrorsMap[requestError.Key()] = &[]string{requestError.Error()} + } return &requestErrorsMap } -// NewResponseFromRequestError creates a new fail response from a request error -func NewResponseFromRequestError( - requestError RequestError, +// NewResponseFromFailRequestError creates a new fail response from a fail request error +func NewResponseFromFailRequestError( + requestError FailRequestError, ) *FailResponse { return NewJSendFailResponse( - NewRequestErrorBodyData(requestError), + NewRequestErrorsBodyData(requestError), requestError.ErrorCode(), requestError.HTTPStatus(), )