Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose HandleHTTPError for use WithErrorHandler option #402

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading