Skip to content

feat(errors): ErrorModel implement errors.Unwrap #777

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 error.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ type ErrorModel struct {
// Errors provides an optional mechanism of passing additional error details
// as a list.
Errors []*ErrorDetail `json:"errors,omitempty" doc:"Optional list of individual error details"`

errors []error
}

// Unwrap satisfies the `error113` interface. It returns an array of wrapped errors.
func (e ErrorModel) Unwrap() []error { return e.errors }

// Error satisfies the `error` interface. It returns the error's detail field.
func (e *ErrorModel) Error() string {
return e.Detail
Expand All @@ -107,8 +112,11 @@ func (e *ErrorModel) Error() string {
// Value: 5
// })
func (e *ErrorModel) Add(err error) {
e.errors = append(e.errors, err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but huma.NewError doesn't actually call Add on the error when constructing the details, and the same is potentially the case when manually returning error models in other code (and handlers in your service). I wonder if there is a better way to support most uses instead of requiring Add to get called 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe smth like errors.Joins on already existing .Errors field ?

Copy link
Author

@burgesQ burgesQ Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a simple edit to the NewError method would already help a bit nvm it's already published with such change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe I think we're good !
My changes were for some code that did some stuff like return nil, huma.Error500...("error", ErrorSpecialType{err}). Turn out my "middleware" tests pass if I change it to smth like return nil, ErrorSpecialType{huma.Error500...("error", err)


if converted, ok := err.(ErrorDetailer); ok {
e.Errors = append(e.Errors, converted.ErrorDetail())

return
}

Expand Down Expand Up @@ -245,6 +253,7 @@ var NewError = func(status int, msg string, errs ...error) StatusError {
Title: http.StatusText(status),
Detail: msg,
Errors: details,
errors: errs,
}
}

Expand Down
Loading