fault

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 4 Imported by: ? modules, 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 fault defines transport-neutral semantic errors: a Kind, the Provider interface that exposes one, and Error, the error a domain layer returns without naming a transport status.

Feature packages expose a Kind through Provider without importing the HTTP or future gRPC policy layers, and a transport maps the same kind to its own status. A fault without a code of its own is rendered with its kind as the code. A service that names its own code returns an Error:

var ErrDomainTaken = fault.New(fault.KindAlreadyExists, "tenant_domain_taken")

The handler returns it unchanged; the transport gives it the kind's status, renders its code and details, and logs its internal cause. A status that only the HTTP boundary knows stays a credo.HTTPError.

The package carries no status and no message key: the client message is resolved from the code by the transport.

Maturity: beta

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v0.26.0

type Error struct {
	// Kind is the semantic category; a transport maps it to its status.
	Kind Kind

	// Code is the client-visible machine code, ^[a-z0-9]+(_[a-z0-9]+)*$.
	// [New] sets the kind as the code when none is given.
	Code string

	// Details carries optional structured, client-safe detail. Never place
	// secrets or internal state here.
	Details any

	// Internal is the underlying cause. It is logged and never rendered.
	Internal error
}

Error is the error a domain layer returns without naming a transport status: a Kind, a client-visible machine code, optional client-safe details and an internal cause.

var ErrDomainTaken = fault.New(fault.KindAlreadyExists, "tenant_domain_taken")

return nil, ErrDomainTaken.WithDetails(map[string]any{"domain": in.Domain}).WithInternal(err)

A transport maps the kind to its status, as for every Provider; Credo's HTTP pipeline renders Code as the code, Details as the details and the message it resolves from the code, and logs Internal, which it never renders. An Error wins over a validation or bind error it wraps, so a cause given as Internal never reaches the client.

A value built without New whose Kind is not declared or whose Code is not a valid machine code fails closed: the HTTP pipeline renders a generic 500.

func New added in v0.26.0

func New(kind Kind, code ...string) *Error

New returns an Error of kind with code as its machine code, or the kind itself as the code when none is given, as NewHTTPError derives a code from its status.

Misuse panics, as a developer invariant violation: a kind that is not one of the declared kinds, KindUnknown included, more than one code, or a code that fails the machine-code grammar ^[a-z0-9]+(_[a-z0-9]+)*$. During a request, recovery turns the panic into a generic 500.

func (*Error) Error added in v0.26.0

func (e *Error) Error() string

Error implements the error interface. The diagnostic order is kind, code, internal; empty segments are omitted.

func (*Error) FaultKind added in v0.26.0

func (e *Error) FaultKind() Kind

FaultKind implements Provider.

func (*Error) Is added in v0.26.0

func (e *Error) Is(target error) bool

Is reports whether target is an Error of the same kind and code, so a copy derived with Error.WithDetails or Error.WithInternal still matches its sentinel.

func (*Error) Unwrap added in v0.26.0

func (e *Error) Unwrap() error

Unwrap returns the internal cause.

func (*Error) WithDetails added in v0.26.0

func (e *Error) WithDetails(v any) *Error

WithDetails returns a copy of e with client-safe details; e is unchanged.

func (*Error) WithInternal added in v0.26.0

func (e *Error) WithInternal(err error) *Error

WithInternal returns a copy of e with the internal cause set; e is unchanged.

type Kind

type Kind string

Kind identifies the semantic category of an error independently of any transport status code.

const (
	KindUnknown       Kind = ""
	KindNotFound      Kind = "not_found"
	KindAlreadyExists Kind = "already_exists"
	KindConstraint    Kind = "constraint"
	KindSerialization Kind = "serialization"
	KindDeadlock      Kind = "deadlock"
	KindContention    Kind = "contention"
	KindConflict      Kind = "conflict"
	KindTimeout       Kind = "timeout"
	KindUnavailable   Kind = "unavailable"
	KindReadOnly      Kind = "read_only"
)

Semantic error kinds understood by Credo's default transport policies.

func KindOf

func KindOf(err error) (Kind, bool)

KindOf finds the first semantic Kind in err's unwrap/join tree.

type Provider

type Provider interface {
	error
	FaultKind() Kind
}

Provider is implemented by errors that expose a transport-neutral semantic kind. Implementations must keep sensitive driver metadata out of FaultKind.

func ProviderOf

func ProviderOf(err error) (Provider, bool)

ProviderOf finds the first non-nil Provider in err's unwrap/join tree. A provider returning KindUnknown is still returned so transport policy can fail closed instead of falling through to an unrelated legacy interface.

Jump to

Keyboard shortcuts

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