Skip to content

Commit 18555a5

Browse files
committed
feat(errors): ErrorModel implement errors.Unwrap
Allow compatibility for errors returned by huma.NewError* with the std' errors.As/errors.Is
1 parent 4977a7a commit 18555a5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

error.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ type ErrorModel struct {
8989
// Errors provides an optional mechanism of passing additional error details
9090
// as a list.
9191
Errors []*ErrorDetail `json:"errors,omitempty" doc:"Optional list of individual error details"`
92+
93+
errors []error
9294
}
9395

96+
// Unwrap satisfies the `error113` interface. It returns an array of wrapper errors.
97+
func (e ErrorModel) Unwrap() []error { return e.errors }
98+
9499
// Error satisfies the `error` interface. It returns the error's detail field.
95100
func (e *ErrorModel) Error() string {
96101
return e.Detail
@@ -107,8 +112,11 @@ func (e *ErrorModel) Error() string {
107112
// Value: 5
108113
// })
109114
func (e *ErrorModel) Add(err error) {
115+
e.errors = append(e.errors, err)
116+
110117
if converted, ok := err.(ErrorDetailer); ok {
111118
e.Errors = append(e.Errors, converted.ErrorDetail())
119+
112120
return
113121
}
114122

@@ -245,6 +253,7 @@ var NewError = func(status int, msg string, errs ...error) StatusError {
245253
Title: http.StatusText(status),
246254
Detail: msg,
247255
Errors: details,
256+
errors: errs,
248257
}
249258
}
250259

0 commit comments

Comments
 (0)