Skip to content

Commit

Permalink
feat: expose HandleHTTPError for use WithErrorHandler option
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim authored Feb 14, 2025
2 parents 7269f44 + 9a4a4ea commit c21f647
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
9 changes: 9 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ func TestWithErrorHandler(t *testing.T) {
errResponse := e.ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
})
t.Run("with HandleHTTPError", func(t *testing.T) {
e := NewEngine(
WithErrorHandler(HandleHTTPError),
)
err := errors.New("Not Found :c")
errResponse := e.ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, errResponse, "500 Internal Server Error")
})
t.Run("custom handler", func(t *testing.T) {
e := NewEngine(
WithErrorHandler(func(err error) error {
Expand Down
14 changes: 11 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,29 @@ func (e NotAcceptableError) Unwrap() error { return HTTPError(e) }
// ErrorHandler is the default error handler used by the framework.
// If the error is an [HTTPError] that error is returned.
// If the error adheres to the [ErrorWithStatus] and/or [ErrorWithDetail] interface
// the error is transformed to a [HTTPError].
// the error is transformed to a [HTTPError] using [HandleHTTPError].
// If the error is not an [HTTPError] nor does it adhere to an
// interface the error is returned as is.
func ErrorHandler(err error) error {
var errorStatus ErrorWithStatus
switch {
case errors.As(err, &HTTPError{}),
errors.As(err, &errorStatus):
return handleHTTPError(err)
return HandleHTTPError(err)
}

return err
}

func handleHTTPError(err error) HTTPError {
// HandleHTTPError is the core logic
// of handling fuego [HTTPError]'s. This
// function takes any error and coerces it into a fuego HTTPError.
// This can be used override the default handler:
//
// engine := fuego.NewEngine(
// WithErrorHandler(HandleHTTPError),
// )
func HandleHTTPError(err error) error {
errResponse := HTTPError{
Err: err,
}
Expand Down
22 changes: 22 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ func TestErrorHandler(t *testing.T) {
})
}

func TestHandleHTTPError(t *testing.T) {
t.Run("should always be HTTPError", func(t *testing.T) {
err := errors.New("test error")

errResponse := HandleHTTPError(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, errResponse, "500 Internal Server Error")
})

t.Run("not found error", func(t *testing.T) {
err := NotFoundError{
Err: errors.New("Not Found :c"),
}
errResponse := HandleHTTPError(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, err, "Not Found :c")
require.ErrorContains(t, errResponse, "Not Found")
require.ErrorContains(t, errResponse, "404")
require.Equal(t, http.StatusNotFound, errResponse.(HTTPError).StatusCode())
})
}

func TestHTTPError_Error(t *testing.T) {
t.Run("title", func(t *testing.T) {
t.Run("custom title", func(t *testing.T) {
Expand Down

0 comments on commit c21f647

Please sign in to comment.