Help us shape the future of Go! Take the 2026 Go Developer Survey

fault

package
v1.0.0-rc.12 Latest Latest
Warning

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

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

Documentation

Overview

Package fault classifies errors by what the caller can do about them, so every transport answers the same failure the same way. An owner marks its own errors with a Kind; HTTP maps a Kind to a status and MCP to a readable refusal, each in one place, instead of every handler re-deriving the table.

The Kind describes the failure, not the transport: NotFound, not 404.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeOf

func CodeOf(err error) string

CodeOf returns the code of the first coded error in err's chain, or "".

func Message

func Message(err error) string

Message is what the caller should read. A sentinel made with New only names a category, and the text wrapped around it carries the specifics, so the whole chain reads. An owner's own error type (a denial, a refused retry) is complete in itself, so only its words read, without the context layers added on the way out.

func New

func New(kind Kind, msg string) error

New returns a classified error with a message, for an owner's sentinel:

var ErrConflict = fault.New(fault.Conflict, "the record changed")

errors.Is still matches the sentinel by identity.

func Wrap

func Wrap(kind Kind, err error) error

Wrap classifies err, keeping it in the chain. A nil err stays nil.

Types

type Classified

type Classified interface {
	error
	FaultKind() Kind
}

Classified is implemented by an error that knows its Kind. Owners with their own error types (a denial with a code, a conflict with detail) implement it rather than wrapping.

type Coded

type Coded interface {
	error
	FaultCode() string
}

Coded is implemented by an error that carries a stable machine code, such as a denial's reason code, for clients that branch on it.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is an error with a Kind. It wraps an underlying error when there is one.

func (*Error) Error

func (e *Error) Error() string

func (*Error) FaultKind

func (e *Error) FaultKind() Kind

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Kind

type Kind uint8

Kind is what went wrong, from the caller's side.

const (
	// Internal is an unclassified failure: the caller cannot fix it.
	Internal Kind = iota
	// Invalid input: the caller can correct the request.
	Invalid
	// Unauthenticated: no verified caller.
	Unauthenticated
	// Forbidden: the caller is known and may not do this.
	Forbidden
	// NotFound: the thing addressed does not exist (or is not visible).
	NotFound
	// Conflict: the request clashes with the current state; retrying after
	// reading it again may succeed.
	Conflict
	// Unprocessable: well-formed, but the current state cannot accept it.
	Unprocessable
	// Unavailable: a dependency is down or busy; retry later.
	Unavailable
)

func KindOf

func KindOf(err error) Kind

KindOf reports the Kind of the first classified error in err's chain, or Internal when nothing in the chain says.

func (Kind) String

func (k Kind) String() string

Jump to

Keyboard shortcuts

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