Documentation
¶
Overview ¶
Package problem implements RFC 9457 "Problem Details for HTTP APIs".
Index ¶
- Constants
- func MustBeAfter(other string) string
- func MustBeAtLeast(n int64) string
- func MustBeAtMost(n int64) string
- func MustBeBetween(least, most int) string
- func MustBeOneOf(values ...string) string
- func Write(w http.ResponseWriter, r *http.Request, p *Problem)
- type Problem
- func BadRequest(detail string) *Problem
- func Conflict(detail string) *Problem
- func Forbidden() *Problem
- func Internal() *Problem
- func InvalidField(field, message string) *Problem
- func MethodNotAllowed(detail string) *Problem
- func New(status int, detail string) *Problem
- func NotFound(detail string) *Problem
- func StaleVersion() *Problem
- func Unauthorized() *Problem
- func Validation(violations ...Violation) *Problem
- type Violation
Constants ¶
const ( MustNotBeBlank = "Must not be blank" MustNotBeEmpty = "Must not be empty" InvalidValue = "Invalid value" ValidEmail = "Must be a valid email address" ValidURL = "Must be a valid web address" BadFormat = "Is not in the expected format" MustBeMoreThan0 = "Must be more than 0" MustNotBeNegative = "Must not be negative" MustBeInThePast = "Must be in the past" MustBeInTheFuture = "Must be in the future" MustNotBeInFuture = "Must not be in the future" MustNotBeInPast = "Must not be in the past" )
The words roux's forms show for the common rules, so every endpoint speaks to the person filling in a form the same way, and in the browser's words (contracts/messages.json is the list both sides test against).
const ContentType = "application/problem+json"
ContentType is the media type for problem details responses.
Variables ¶
This section is empty.
Functions ¶
func MustBeAfter ¶
MustBeAfter is the message for a value not after another, named as the form labels it: "Must be after starts at".
func MustBeAtLeast ¶
MustBeAtLeast is the message for a number under its minimum: "Must be at least 5".
func MustBeAtMost ¶
MustBeAtMost is the message for a number over its maximum: "Must be at most 5".
func MustBeBetween ¶
MustBeBetween is the message for a length out of range: "Must be between 1 and 80 characters".
func MustBeOneOf ¶
MustBeOneOf is the message for a value outside a fixed set: "Must be one of A, B".
Types ¶
type Problem ¶
type Problem struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Status int `json:"status,omitempty"`
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
Errors []Violation `json:"errors,omitempty"` // extension member
TraceID string `json:"traceId,omitempty"` // extension member: correlates with logs/traces
Code string `json:"code,omitempty"` // extension member: a stable machine-readable error code
ErrorID string `json:"errorId,omitempty"` // extension member on a 500: matches the response to the log entry
// Headers are sent with the response (WWW-Authenticate on a 401, say); never in the body.
Headers http.Header `json:"-"`
}
Problem is an RFC 9457 problem details object. It also implements error, so handlers can simply return one.
func BadRequest ¶
func InvalidField ¶
InvalidField is a Validation with one field's problem.
func MethodNotAllowed ¶
func StaleVersion ¶
func StaleVersion() *Problem
StaleVersion is the 409 for an edit made from a version that is no longer current.
func Unauthorized ¶
func Unauthorized() *Problem
func Validation ¶
Validation returns the 400 roux answers for invalid input: the fields that are wrong, sorted by field and then message so a response is stable.
func (*Problem) WithHeader ¶
WithHeader adds a response header to the problem and returns it.
func (*Problem) WriteHeaders ¶
func (p *Problem) WriteHeaders(w http.ResponseWriter)
WriteHeaders sends the problem's own headers; a renderer that writes its own body calls it.