Skip to content

Commit

Permalink
refactor: added ErrorCode and HTTPStatus getters to RequestError inte…
Browse files Browse the repository at this point in the history
…rface
  • Loading branch information
ralvarezdev committed Jan 14, 2025
1 parent 82b8a6a commit c9db186
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 105 deletions.
64 changes: 19 additions & 45 deletions http/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,14 @@ type (
dest interface{},
validatorFn func(body interface{}) (interface{}, error),
) bool
HandleError(
w http.ResponseWriter,
err error,
httpStatus int,
)
HandleResponse(
w http.ResponseWriter,
response *gonethttpresponse.Response,
)
HandleErrorProneResponse(
HandleError(
w http.ResponseWriter,
successResponse *gonethttpresponse.Response,
errorResponse *gonethttpresponse.Response,
err error,
httpStatus int,
)
}

Expand Down Expand Up @@ -139,34 +134,6 @@ func (d *DefaultHandler) DecodeAndValidate(
return d.Validate(w, body, validatorFn)
}

// HandleError handles the error response
func (d *DefaultHandler) HandleError(
w http.ResponseWriter,
err error,
httpStatus int,
) {
// Check if the errors is a request error
var e gonethttpresponse.RequestError
if errors.As(err, &e) {
d.HandleResponse(
w, gonethttpresponse.NewFailResponse(
gonethttpresponse.NewRequestErrorsBodyData(e),
nil,
httpStatus,
),
)
return
}

d.HandleResponse(
w, gonethttpresponse.NewDebugErrorResponse(
gonethttpstatuserrors.InternalServerError,
err,
nil, nil, http.StatusInternalServerError,
),
)
}

// HandleResponse handles the response
func (d *DefaultHandler) HandleResponse(
w http.ResponseWriter,
Expand All @@ -190,18 +157,25 @@ func (d *DefaultHandler) HandleResponse(
_ = d.jsonEncoder.Encode(w, response)
}

// HandleErrorProneResponse handles the response that may contain an error
func (d *DefaultHandler) HandleErrorProneResponse(
// HandleError handles the error response
func (d *DefaultHandler) HandleError(
w http.ResponseWriter,
successResponse *gonethttpresponse.Response,
errorResponse *gonethttpresponse.Response,
err error,
) {
// Check if the error response is nil
if errorResponse != nil {
d.HandleResponse(w, errorResponse)
// Check if the errors is a request error
var e gonethttpresponse.RequestError
if errors.As(err, &e) {
d.HandleResponse(
w, gonethttpresponse.NewFailResponseFromRequestError(e),
)
return
}

// Handle the success response
d.HandleResponse(w, successResponse)
d.HandleResponse(
w, gonethttpresponse.NewDebugErrorResponse(
gonethttpstatuserrors.InternalServerError,
err,
nil, nil, http.StatusInternalServerError,
),
)
}
13 changes: 5 additions & 8 deletions http/jwt/validator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ func NewDefaultFailHandler(
) {
// Encode the response
_ = jsonEncoder.Encode(
w, gonethttpresponse.NewFailResponse(
gonethttpresponse.NewRequestErrorsBodyData(
gonethttpresponse.NewHeaderError(
gojwtnethttp.AuthorizationHeaderKey,
error.Error(),
),
w, gonethttpresponse.NewFailResponseFromRequestError(
gonethttpresponse.NewHeaderError(
gojwtnethttp.AuthorizationHeaderKey,
error.Error(),
http.StatusUnauthorized,
),
nil,
http.StatusUnauthorized,
),
)
}, nil
Expand Down
Loading

0 comments on commit c9db186

Please sign in to comment.