Documentation
¶
Overview ¶
Package humaproblem makes huma produce problem details: every error huma builds, including request validation, becomes a *problem.Problem, and the OpenAPI document lists the codes clients localize on.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FieldErrors ¶
func FieldErrors(errs []error) []*problem.FieldError
FieldErrors converts huma's error details into field errors, each with the rule it failed and that rule's English message as its detail, never huma's text. A *problem.FieldError among errs, such as one a Resolver returns, is kept as is; an error that locates no field is skipped.
func Install ¶
func Install(opts Options)
Install makes huma build its errors with NewError. Call it at startup, before registering operations: huma also reads the error type from it to document error responses. Calling it again, as tests do, only replaces the options, so it is safe while other goroutines serve requests.
func Localize ¶ added in v0.11.0
func Localize(l *problem.Localizer) huma.Transformer
Localize returns a huma Transformer that writes every problem the API sends in the reader's language with l, and says which in Content-Language. Add it to huma.Config.Transformers after Report, which logs the original.
func NewError ¶
func NewError(status int, msg string, errs ...error) huma.StatusError
NewError converts an error huma reports into a problem. A problem among errs is returned as is, and so is one Options.Map returns for them. A 422 with field details becomes a problem.Validation listing them, each typed with the rule it failed. Anything else is an about:blank problem with no detail, so a client shows its status's message.
Nothing huma writes is sent: msg and errs, such as encoding/json's text for a malformed body or a validator's English for a field, become the problem's cause, for logs. That includes the message given to huma's own constructors, such as huma.Error404NotFound("..."): to send words of your own, return a problem, such as problem.Status(http.StatusNotFound, "no such team").
Types ¶
type Options ¶
type Options struct {
// Map turns an error a handler returned that is not a problem, such as a
// database's not-found error, into one. It returns nil for an error it does
// not recognize, which becomes a 500. The error becomes the problem's cause.
Map func(error) *problem.Problem
// Debug sends a 5xx problem's cause as its detail. Never set it in production.
Debug bool
}
Options configure Install.