Documentation
¶
Overview ¶
Package errors is the one way domain and application code says what went wrong: an error of a fixed kind with a stable code. Inbound adapters map the kind to their protocol in one place (HTTP answers a problem document), so no handler invents a status of its own and every API answers an error alike.
An error that carries no kind is an unexpected failure: adapters answer it as internal, with no detail, and log its cause.
Index ¶
- func IsKind(err error, k Kind) bool
- type Error
- func Conflict(code, detail string) *Error
- func Forbidden(code, detail string) *Error
- func From(err error) (*Error, bool)
- func Gone(code, detail string) *Error
- func Invalid(code, detail string, violations ...Violation) *Error
- func NotFound(code, detail string) *Error
- func PreconditionFailed(code, detail string) *Error
- func RateLimited(code, detail string, retryAfter time.Duration) *Error
- func Unauthenticated(code, detail string) *Error
- func Unavailable(code, detail string) *Error
- type Kind
- type Violation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
type Error struct {
Kind Kind
// Code is stable and snake_case: clients branch on it, so it never changes
// once released.
Code string
// Detail explains this occurrence to a human.
Detail string
// Violations are the fields that break a rule, for KindInvalid.
Violations []Violation
// Extensions are code-specific members a client may read, such as the
// references that block a deletion.
Extensions map[string]any
// RetryAfter is how long to wait before retrying, for KindRateLimited
// and, when known, KindUnavailable.
RetryAfter time.Duration
// contains filtered or unexported fields
}
Error is an error of a kind. Its fields other than the cause are what a client is told; the cause is for the log alone.
func PreconditionFailed ¶
PreconditionFailed reports a write based on an out-of-date view of the entity.
func RateLimited ¶
RateLimited reports too many attempts, and how long to wait before the next.
func Unauthenticated ¶
Unauthenticated reports a caller that is not known.
func Unavailable ¶
Unavailable reports a dependency failure worth retrying later.
func (*Error) Under ¶
Under returns a copy of e whose violations point under prefix, a JSON Pointer: for what was checked as a document of its own and arrived as part of a larger request body.
func (*Error) WithCause ¶
WithCause returns a copy of e wrapping cause, which is logged and never sent to a client.
func (*Error) WithExtension ¶
WithExtension returns a copy of e with one more extension member.
type Kind ¶
type Kind int
Kind is the class of an error, the part an adapter maps to a status.
const ( // KindInvalid: the input breaks a rule; field violations say where. KindInvalid Kind // KindNotFound: the entity does not exist, or the caller may not know it does. KindNotFound // KindConflict: the change contradicts the current state, such as a key already taken. KindConflict // KindForbidden: the caller is known but may not do this. KindForbidden // KindUnauthenticated: the caller is not known. KindUnauthenticated // KindPreconditionFailed: the caller's view of the entity is out of date. KindPreconditionFailed // KindGone: the entity existed and no longer can be used, such as an expired invitation. KindGone // KindRateLimited: too many attempts; the error carries how long to wait. KindRateLimited KindUnavailable )
The kinds. The zero value is no kind, so a forgotten field is never mistaken for one of them.
type Violation ¶
type Violation struct {
// Pointer is a JSON Pointer (RFC 6901) into the request body, such as
// "/variations/1/value".
Pointer string
// Code is the stable snake_case name of the rule broken.
Code string
// Detail explains the violation to a human.
Detail string
// Params are the values of the rule broken, such as the bounds of a
// length, so a client explains the violation in its own words and
// language from the code and these; nil when the rule has none.
Params map[string]any
}
Violation is one field of a request that breaks a rule.