Skip to content

Commit

Permalink
refactor: renamed RequestError as FailRequestError
Browse files Browse the repository at this point in the history
* Renamed RequestError as FailRequestError
* Now, CookieError implements the FailRequestError interface
  • Loading branch information
ralvarezdev committed Feb 2, 2025
1 parent 6b3d80c commit a2b6a95
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
6 changes: 3 additions & 3 deletions http/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion http/middleware/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Authenticator interface
type Authenticator interface {
Authenticate(
errorHandler func(
failHandler func(
w http.ResponseWriter,
err string,
httpStatus int,
Expand Down
20 changes: 10 additions & 10 deletions http/middleware/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -58,7 +58,7 @@ func (m *Middleware) Authenticate(
token,
)
if err != nil {
errorHandler(
failHandler(
w,
err.Error(),
http.StatusUnauthorized,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package response

type (
// RequestError struct
RequestError interface {
// FailRequestError struct
FailRequestError interface {
Key() string
Error() string
HTTPStatus() int
Expand Down Expand Up @@ -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
Expand All @@ -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(),
)
Expand Down

0 comments on commit a2b6a95

Please sign in to comment.