errors

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package errors is wowapi's error taxonomy: a closed set of Kinds that map deterministically to HTTP status codes and stable machine codes, plus the structured Error type carried across every layer. The HTTP layer (kernel/ httpx) translates an *Error into an RFC 9457 problem-details body; anything that is not an *Error becomes an opaque 500 whose detail never reaches the wire. Contract: docs/blueprint/04 §5.

The taxonomy is intentionally closed — new failure modes pick an existing Kind rather than inventing wire contracts ad hoc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Op

func Op(op string) opString

Op tags an *Error with the operation name for logs.

Types

type Error

type Error struct {
	Kind   Kind
	Code   string // stable machine code; defaults to Kind.DefaultCode()
	Msg    string // safe, user-facing
	Op     string // "requests.Service.Approve" — logs only
	Fields []FieldError
	Err    error // wrapped cause (%w)
}

Error is the structured error carried across layers. Msg is user-safe; Op and the wrapped Err are for logs only and never reach the wire.

func As

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

As is a convenience wrapper over errors.As for *Error.

func E

func E(kind Kind, code, msg string, args ...any) *Error

E constructs an *Error. msg is treated as a plain string (not a format string) so caller-supplied values can never turn into format verbs; use fmt.Sprintf at the call site if you need interpolation. args, if given, may be a single wrapped error and/or an Op string:

errors.E(KindNotFound, "not_found", "request not found")
errors.E(KindInternal, "internal", "load failed", cause, Op("svc.Load"))

func Validation

func Validation(msg string, fields ...FieldError) *Error

Validation builds a KindValidation error carrying field errors.

func Wrapf

func Wrapf(err error, op, format string, a ...any) *Error

Wrapf wraps err with an operation prefix while preserving the taxonomy Kind of an underlying *Error (or KindInternal). It is the "every layer wraps" convention from 04 §5 without flattening the Kind.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap exposes the wrapped cause to errors.Is/As.

type FieldError

type FieldError struct {
	Field   string `json:"field"`   // JSON path, e.g. "contacts[0].email"
	Code    string `json:"code"`    // "required", "max_length", "invalid_format"
	Message string `json:"message"` // safe for users
}

FieldError is one shape-validation failure, addressed by JSON path.

type Kind

type Kind int

Kind is the closed set of error categories. Each maps to exactly one HTTP status and one machine code (see mapping below).

const (
	KindInternal Kind = iota // default zero value: unmapped → 500
	KindValidation
	KindUnauthenticated
	KindForbidden
	KindTenantIsolation
	KindNotFound
	KindConflict
	KindVersionConflict
	KindIdempotencyInFlight
	KindRuleViolation
	KindWorkflowState
	KindRateLimited
	KindExternal
	// KindIdempotencyExpired: a request presented an idempotency key whose
	// stored record has expired, so the original response can no longer be
	// replayed. Returned instead of silently re-executing the operation
	// (roadmap S5). Appended last to keep the earlier iota values stable.
	KindIdempotencyExpired
)

func KindOf

func KindOf(err error) Kind

KindOf extracts the taxonomy Kind for any error: the nearest wrapped *Error's Kind, or KindInternal when none is present (unknown errors are 500s).

func (Kind) DefaultCode

func (k Kind) DefaultCode() string

DefaultCode returns the taxonomy's machine code for k.

func (Kind) HTTPStatus

func (k Kind) HTTPStatus() int

HTTPStatus returns the HTTP status k maps to.

Jump to

Keyboard shortcuts

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