Documentation
¶
Index ¶
- Variables
- func Bind(c echo.Context, schema z.Schema, i any) error
- func ProblemHandler(err error, c echo.Context)
- type ContentTypeFilter
- type ErrorDetail
- type Problem
- func Error400BadRequest(msg string, errs ...error) *Problem
- func Error401Unauthorized(msg string, errs ...error) *Problem
- func Error403Forbidden(msg string, errs ...error) *Problem
- func Error404NotFound(msg string, errs ...error) *Problem
- func Error405MethodNotAllowed(msg string, errs ...error) *Problem
- func Error406NotAcceptable(msg string, errs ...error) *Problem
- func Error409Conflict(msg string, errs ...error) *Problem
- func Error410Gone(msg string, errs ...error) *Problem
- func Error412PreconditionFailed(msg string, errs ...error) *Problem
- func Error415UnsupportedMediaType(msg string, errs ...error) *Problem
- func Error422UnprocessableEntity(msg string, errs ...error) *Problem
- func Error429TooManyRequests(msg string, errs ...error) *Problem
- func Error500InternalServerError(msg string, errs ...error) *Problem
- func Error501NotImplemented(msg string, errs ...error) *Problem
- func Error502BadGateway(msg string, errs ...error) *Problem
- func Error503ServiceUnavailable(msg string, errs ...error) *Problem
- func Error504GatewayTimeout(msg string, errs ...error) *Problem
- func Format(errs z.ZogErrMap) *Problem
- func Status304NotModified() *Problem
Constants ¶
This section is empty.
Variables ¶
var NewError = func(status int, msg string, errs ...error) *Problem { details := make([]*ErrorDetail, len(errs)) for i := 0; i < len(errs); i++ { var ed *ErrorDetail if errors.As(errs[i], &ed) { details[i] = ed } else { if errs[i] == nil { continue } details[i] = &ErrorDetail{Message: errs[i].Error()} } } return &Problem{ Status: status, Title: http.StatusText(status), Detail: msg, Errors: details, } }
NewError creates a new instance of an error model with the given status code, message, and optional error details. If the error details implement the `ErrorDetailer` interface, the error details will be used. Otherwise, the error string will be used as the message. This function is used by all the error response utility functions, like `huma.Error400BadRequest`.
Replace this function to use your own error type. Example:
type MyDetail struct { Message string `json:"message"` Location string `json:"location"` } type MyError struct { status int Message string `json:"message"` Errors []error `json:"errors"` } func (e *MyError) Error() string { return e.Message } func (e *MyError) GetStatus() int { return e.status } huma.NewError = func(status int, msg string, errs ...error) StatusError { return &MyError{ status: status, Message: msg, Errors: errs, } }
Functions ¶
func ProblemHandler ¶
func ProblemHandler(err error, c echo.Context)
Types ¶
type ContentTypeFilter ¶
ContentTypeFilter allows you to override the content type for responses, allowing you to return a different content type like `application/problem+json` after using the `application/json` marshaller. This should be implemented by the response body struct.
type ErrorDetail ¶
type ErrorDetail struct { // Message is a human-readable explanation of the error. Message string `json:"message,omitempty" doc:"Error message text"` // Location is a path-like string indicating where the error occurred. // It typically begins with `path`, `query`, `header`, or `body`. Example: // `body.items[3].tags` or `path.thing-id`. Location string `json:"location,omitempty" doc:"Where the error occurred, e.g. 'body.items[3].tags' or 'path.thing-id'"` // Value is the value at the given location, echoed back to the client // to help with debugging. This can be useful for e.g. validating that // the client didn't send extra whitespace or help when the client // did not log an outgoing request. Value any `json:"value,omitempty" doc:"The value at the given location"` }
ErrorDetail provides details about a specific error.
func (*ErrorDetail) Error ¶
func (e *ErrorDetail) Error() string
Error returns the error message / satisfies the `error` interface. If a location and value are set, they will be included in the error message, otherwise just the message is returned.
func (*ErrorDetail) ErrorDetail ¶
func (e *ErrorDetail) ErrorDetail() *ErrorDetail
ErrorDetail satisfies the `ErrorDetailer` interface.
type Problem ¶
type Problem struct { // Type is a URI to get more information about the error type. Type string `` /* 170-byte string literal not displayed */ // Title provides a short static summary of the problem. Huma will default this // to the HTTP response status code text if not present. Title string `` /* 166-byte string literal not displayed */ // Status provides the HTTP status code for client convenience. Huma will // default this to the response status code if unset. This SHOULD match the // response status code (though proxies may modify the actual status code). Status int `json:"status,omitempty" example:"400" doc:"HTTP status code"` // Detail is an explanation specific to this error occurrence. Detail string `` /* 153-byte string literal not displayed */ // Instance is a URI to get more info about this error occurrence. Instance string `` /* 163-byte string literal not displayed */ // 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"` }
Problem defines a basic error message model based on RFC 9457 Problem Details for HTTP APIs (https://datatracker.ietf.org/doc/html/rfc9457). It is augmented with an `errors` field of `huma.ErrorDetail` objects that can help provide exhaustive & descriptive errors.
err := &Problem{ Title: http.StatusText(http.StatusBadRequest), Status: http.StatusBadRequest, Detail: "Validation failed", Errors: []*ErrorDetail{ &ErrorDetail{ Message: "expected required property id to be present", Location: "body.friends[0]", Value: nil, }, &ErrorDetail{ Message: "expected boolean", Location: "body.friends[1].active", Value: 5, }, }, }
func Error400BadRequest ¶
Error400BadRequest returns a 400.
func Error401Unauthorized ¶
Error401Unauthorized returns a 401.
func Error403Forbidden ¶
Error403Forbidden returns a 403.
func Error404NotFound ¶
Error404NotFound returns a 404.
func Error405MethodNotAllowed ¶
Error405MethodNotAllowed returns a 405.
func Error406NotAcceptable ¶
Error406NotAcceptable returns a 406.
func Error409Conflict ¶
Error409Conflict returns a 409.
func Error410Gone ¶
Error410Gone returns a 410.
func Error412PreconditionFailed ¶
Error412PreconditionFailed returns a 412.
func Error415UnsupportedMediaType ¶
Error415UnsupportedMediaType returns a 415.
func Error422UnprocessableEntity ¶
Error422UnprocessableEntity returns a 422.
func Error429TooManyRequests ¶
Error429TooManyRequests returns a 429.
func Error500InternalServerError ¶
Error500InternalServerError returns a 500.
func Error501NotImplemented ¶
Error501NotImplemented returns a 501.
func Error502BadGateway ¶
Error502BadGateway returns a 502.
func Error503ServiceUnavailable ¶
Error503ServiceUnavailable returns a 503.
func Error504GatewayTimeout ¶
Error504GatewayTimeout returns a 504.
func Status304NotModified ¶
func Status304NotModified() *Problem
Status304NotModified returns a 304. This is not really an error, but provides a way to send non-default responses.
func (*Problem) Add ¶
Add an error to the `Errors` slice. If passed a struct that satisfies the `huma.ErrorDetailer` interface, then it is used, otherwise the error string is used as the error detail message.
err := &Problem{ /* ... */ } err.Add(&ErrorDetail{ Message: "expected boolean", Location: "body.friends[1].active", Value: 5 })
func (*Problem) ContentType ¶
ContentType provides a filter to adjust response content types. This is used to ensure e.g. `application/problem+json` content types defined in RFC 9457 Problem Details for HTTP APIs are used in responses to clients.