Set Type
in the ErrorModel
#360
Replies: 11 comments
-
@victoraugustolls I don't understand, maybe a code example of what you are trying to do would help? You should be able to just create a
|
Beta Was this translation helpful? Give feedback.
-
I understand that the errors don't need to be RFC9457! But if it is, the type ErrorModel struct {
huma.ErrorModel
}
func (e *ErrorModel) ContentType(ct string) string {
if ct == "application/json" {
return "application/json"
}
return ct
}
type TypeErr string
func (e TypeErr) Error() string {
return string(e)
}
var NewErrorWithType = func(status int, msg string, errs ...error) huma.StatusError {
var details []*huma.ErrorDetail
var (
tp string
typeErr TypeErr
)
for i := 0; i < len(errs); i++ {
if errs[i] == nil {
continue
}
if converted, ok := errs[i].(huma.ErrorDetailer); ok {
details = append(details, converted.ErrorDetail())
} else if errors.As(errs[i], &typeErr) {
tp = typeErr.Error()
} else {
details = append(details, &huma.ErrorDetail{Message: errs[i].Error()})
}
}
return &ErrorModel{
huma.ErrorModel{
Type: tp,
Status: status,
Title: http.StatusText(status),
Detail: msg,
Errors: details,
},
}
} |
Beta Was this translation helpful? Give feedback.
-
If that is the expected behavior, that's ok! But I feel |
Beta Was this translation helpful? Give feedback.
-
@victoraugustolls how do you use your |
Beta Was this translation helpful? Give feedback.
-
@danielgtaylor exactly! And don't pass the error to the details, only to the
I wrapped the |
Beta Was this translation helpful? Give feedback.
-
@victoraugustolls I do wonder if we are overcomplicating this. Compare it to: err := huma.Error403Forbidden("invalid action for token", err)
err.Type = "srn:error:unauthorized" This way you can also set the |
Beta Was this translation helpful? Give feedback.
-
@danielgtaylor, but type StatusError interface {
GetStatus() int
Error() string
} I do prefer to be able to set it up directly, as you proposed! It's way simpler. |
Beta Was this translation helpful? Give feedback.
-
Also, the only reason that I am wrapping the |
Beta Was this translation helpful? Give feedback.
-
@victoraugustolls yeah you would have to cast, which is a bit annoying to be honest, I guess like: err := huma.Error403Forbidden("invalid action for token", err).(*huma.ErrorModel)
err.Type = "srn:error:unauthorized" Alternatively you can write your own utility functions for the different error responses.
No, this looks like a reasonable way to do it. 👍 |
Beta Was this translation helpful? Give feedback.
-
@danielgtaylor do you feel like there should be an easier way to do both of this operations? If so, I can try of think of something non breaking and suggest it here. |
Beta Was this translation helpful? Give feedback.
-
Moving this to a discussion until we can think up some ways to make it work. In the meantime the easiest solution is probably just to create your own utility methods that behave like |
Beta Was this translation helpful? Give feedback.
-
Right now, I see no way to directly set the
Type
of the error as the interfaceStatusError
doesn't include a method, andhuma.NewError
also doesn't accept atype
parameter! Maybe there is something obvious that I'm missing, but right now, I'm passing a custom error type toNewError
just to set this field!@danielgtaylor WDYT of maybe creating a
NewErrorWithType
or adding a method forStatusError
to set the type?Beta Was this translation helpful? Give feedback.
All reactions