errors

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsKind

func IsKind(err error, k Kind) bool

IsKind reports whether err's chain holds an error of kind k.

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 Conflict

func Conflict(code, detail string) *Error

Conflict reports a change that contradicts the current state.

func Forbidden

func Forbidden(code, detail string) *Error

Forbidden reports a known caller that may not do what it asked.

func From

func From(err error) (*Error, bool)

From returns the first error of a kind in err's chain.

func Gone

func Gone(code, detail string) *Error

Gone reports an entity that existed and can no longer be used.

func Invalid

func Invalid(code, detail string, violations ...Violation) *Error

Invalid reports input that breaks a rule, with the fields that break it.

func NotFound

func NotFound(code, detail string) *Error

NotFound reports an entity that does not exist or that the caller may not know of.

func PreconditionFailed

func PreconditionFailed(code, detail string) *Error

PreconditionFailed reports a write based on an out-of-date view of the entity.

func RateLimited

func RateLimited(code, detail string, retryAfter time.Duration) *Error

RateLimited reports too many attempts, and how long to wait before the next.

func Unauthenticated

func Unauthenticated(code, detail string) *Error

Unauthenticated reports a caller that is not known.

func Unavailable

func Unavailable(code, detail string) *Error

Unavailable reports a dependency failure worth retrying later.

func (*Error) Error

func (e *Error) Error() string

Error describes the error for a log: kind, code, detail and cause.

func (*Error) Under

func (e *Error) Under(prefix string) *Error

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) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the cause.

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

WithCause returns a copy of e wrapping cause, which is logged and never sent to a client.

func (*Error) WithExtension

func (e *Error) WithExtension(key string, value any) *Error

WithExtension returns a copy of e with one more extension member.

func (*Error) WithRetryAfter

func (e *Error) WithRetryAfter(d time.Duration) *Error

WithRetryAfter returns a copy of e saying how long to wait before retrying, for an unavailable error whose wait is known, such as a busy resource.

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: a dependency failed in a way worth retrying later.
	KindUnavailable
)

The kinds. The zero value is no kind, so a forgotten field is never mistaken for one of them.

func (Kind) String

func (k Kind) String() string

String returns the kind's snake_case name, or "none" for the zero value.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL