failure

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package failure classes errors and maps the classes to the shared exit-code table of contract.md section 9.2. A classed error carries a message, the next step the caller should take, and details for the error envelope the CLI writes on stderr.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Code

func Code(class Class) int

Code returns the exit code of a class; an unknown class is Internal's.

func ExitCode

func ExitCode(err error) int

ExitCode returns the exit code for err; 0 for nil.

func Is

func Is(err error, class Class) bool

Is reports whether err has the class.

func New

func New(class Class, message, nextStep string, details ...any) error

New makes a classed error. details are key, value pairs for the envelope. It panics on an empty nextStep: every failure tells the caller what to do next.

func Register

func Register(class Class, code int)

Register adds an app class with its exit code. Codes 9 to 125 are the app's; the shared table owns 0 to 8, 130 and 143. It panics on a code outside that range or on a class or code already taken, because the tool's exit codes would then be ambiguous.

func Wrap

func Wrap(cause error, class Class, message, nextStep string, details ...any) error

Wrap is New with a cause kept for errors.Is and errors.As.

func Write

func Write(w io.Writer, err error) error

Write encodes err as the one-line JSON error envelope of contract.md section 9.2: {"class","message","next_step","details"}. Details that cannot be encoded are dropped; class, message and next step always survive.

Types

type Class

type Class string

Class is the stable failure category. Its string and its code are part of the output contract of every tool.

const (
	OK         Class = "ok"
	Internal   Class = "internal"
	Usage      Class = "usage"
	Auth       Class = "auth"
	Permission Class = "permission"
	NotFound   Class = "not_found"
	Conflict   Class = "conflict"
	Remote     Class = "remote"
	Timeout    Class = "timeout"
	Cancelled  Class = "cancelled"
	Terminated Class = "terminated"
)

The shared classes. Every tool gives them the same meaning and the same exit code.

func ClassOf

func ClassOf(err error) Class

ClassOf returns the class of err: OK for nil, the error's own class when it has one, Cancelled for context.Canceled, Timeout for context.DeadlineExceeded, Internal otherwise.

type Error

type Error struct {
	Class    Class
	Message  string
	NextStep string
	Details  map[string]any
	// contains filtered or unexported fields
}

Error is a classed error.

func From

func From(err error) *Error

From turns any error into a classed Error by the rules of ClassOf. It returns a value with class OK for nil.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the cause given to Wrap.

Jump to

Keyboard shortcuts

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