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
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
Error implements the error interface. The diagnostic order is kind, code, internal; empty segments are omitted.
func (*Error) Is ¶ added in v0.26.0
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) WithDetails ¶ added in v0.26.0
WithDetails returns a copy of e with client-safe details; e is unchanged.
func (*Error) WithInternal ¶ added in v0.26.0
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" KindReadOnly Kind = "read_only" )
Semantic error kinds understood by Credo's default transport policies.
type Provider ¶
Provider is implemented by errors that expose a transport-neutral semantic kind. Implementations must keep sensitive driver metadata out of FaultKind.
func ProviderOf ¶
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.