errs

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package errs wraps internal errors and analyzes them at a boundary.

It is the diagnostic layer only. Public errors are apperror values, which carry the catalog code clients branch on; package errors are sentinels that callers compare with errors.Is. errs adds what a boundary needs to write one useful record per unit of work: where the error was produced (a captured stack), who is at fault, structured attributes, and a redacted rendering of the whole chain.

Traversal follows Unwrap() error, Unwrap() []error and, for errors that implement only Cause() error, Cause(). apperror keeps its cause out of Unwrap on purpose, so errors.Is and errors.As do not cross a public error; Cause() lets diagnostics cross it without changing that.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Answer added in v0.22.0

func Answer(ctx context.Context, err error) (*apperror.Error, apperror.Fault)

Answer is the public error a boundary answers err with, and the fault it attributes err to. ctx is the unit's own context; nil means no caller. Every transport renders its answer from it, in this order:

  1. The caller has ended and the chain holds a cancellation: canceled.
  2. The outermost catalog apperror on the chain, unless it came from a remote that refused a request this process sent and was not forwarded.
  3. The generic code for the fault: http.bad_request for a client fault, internal otherwise.

A native gRPC status takes part in the attribution but is never the answer. The answer of a nil err is nil.

func CallerEnded

func CallerEnded(ctx context.Context) bool

CallerEnded reports whether ctx has ended because its caller canceled it or its deadline passed: its cause is exactly context.Canceled or context.DeadlineExceeded. A context this process ended for its own reason, such as an idle timeout or a lost run, did not end by its caller. A nil ctx has no caller and never ends.

func FaultOf

func FaultOf(err error) apperror.Fault

FaultOf is the fault Analyze attributes err to when no caller has canceled the unit. An RPC server sends it to its client with the error.

func Forwarded

func Forwarded(err error) error

Forwarded declares that the caller deliberately passed end-user input to the downstream, so a remote client fault stays a client fault here.

func New

func New(msg string, attrs ...slog.Attr) error

New creates an internal error attributed to this process by default.

func NewDependency

func NewDependency(msg string, attrs ...slog.Attr) error

NewDependency creates an error attributed to a dependency.

func NewWithDepth

func NewWithDepth(depth int, msg string, attrs ...slog.Attr) error

NewWithDepth is New with the recorded stack starting depth frames further up. A helper that builds one kind of error uses it so the source names the helper's caller.

func Recorded added in v0.22.0

func Recorded(err error) error

Recorded marks a failure another result record in this process has already recorded: a nested unit that wrote its own record when it ended. An outer boundary still records it, at no more than WARN. The error value is unchanged.

func Recovered

func Recovered(v any) error

Recovered turns a value from recover into an internal error with a stack. Call it directly in the deferred function. The stack starts where the panic happened. The panic value can hold user data, so it is not part of the error text; only a runtime error keeps its description.

func RedactURLs

func RedactURLs(text string) string

RedactURLs removes the userinfo, query and fragment of every URL in text. Rendering a chain applies it; text from outside the process (an upstream error message) must go through it before it becomes an attribute. A URL that does not parse keeps only its scheme, since the rest may hold credentials.

func Remote

func Remote(err error) error

Remote marks an error received from an internal RPC. The error value is unchanged: status.FromError and status.Code still read the received status.

func Text

func Text(err error) string

Text returns the redacted text of the whole chain.

func WithTimeout

WithTimeout creates a timeout this process sets for itself. When it expires the context's cause is not context.DeadlineExceeded, so Analyze does not attribute the failure to the caller.

func Wrap

func Wrap(err error, msg string, attrs ...slog.Attr) error

Wrap adds context to err and keeps the chain; errors.Is and errors.As still see err. It returns nil when err is nil.

func WrapDependency

func WrapDependency(err error, msg string, attrs ...slog.Attr) error

WrapDependency wraps err and attributes it to a dependency.

func WrapDependencyWithDepth

func WrapDependencyWithDepth(depth int, err error, msg string, attrs ...slog.Attr) error

WrapDependencyWithDepth is WrapDependency with depth as in WrapWithDepth.

func WrapWithDepth

func WrapWithDepth(depth int, err error, msg string, attrs ...slog.Attr) error

WrapWithDepth is Wrap with the recorded stack starting depth frames further up. A wrapping helper uses it so the source is the helper's caller rather than the helper; depth 0 is Wrap.

Types

type Frame

type Frame struct {
	Function, File string
	Line           int
}

Frame is a source frame where an error was produced.

func (Frame) LogValue

func (f Frame) LogValue() slog.Value

LogValue encodes the frame as a log group.

type Report

type Report struct {
	Fault       apperror.Fault
	Reason      string
	Text        string
	Source      *Frame
	Stack       []Frame
	Attrs       []slog.Attr
	Remote      bool
	RemoteFault apperror.Fault
	Unlocated   bool
	Panic       bool

	// Recorded reports that a nested unit in this process has already
	// recorded this failure (see Recorded).
	Recorded bool
	// contains filtered or unexported fields
}

Report is what Analyze concludes about an error chain at a boundary.

func Analyze

func Analyze(ctx context.Context, err error) Report

Analyze attributes err, returned by a unit of work running under ctx. A nil ctx means the unit has no caller that could have canceled it.

func (Report) LogAttrs

func (r Report) LogAttrs() []slog.Attr

LogAttrs returns the error fields of a result or event record.

Jump to

Keyboard shortcuts

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