Documentation
¶
Overview ¶
Package errors provides small error helpers used across go-service.
This package is intentionally lightweight. It primarily re-exports a subset of the standard library `errors` package APIs (As/Is/Join/New) behind a stable go-service import path, and provides a small convenience helper (`Prefix`) for consistently attributing errors to a subsystem or component.
Re-exports ¶
The following functions mirror the behavior and semantics of the standard library equivalents:
- As: type-assert (via error chain traversal) into a target
- Is: match a target error in an error chain
- Join: combine multiple errors into one
- New: construct a sentinel error value
Prefixing errors ¶
Prefix is commonly used at module/service boundaries to add a stable component prefix to an error message while preserving the original error for unwrapping:
err := errors.Prefix("cache", underlyingErr)
Prefix returns nil when the input error is nil, which makes it convenient to use in return statements without additional nil checks.
Start with `As`, `Is`, `Join`, `New`, and `Prefix`.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v2.14.0
As reports whether any error in err's chain matches target, and if so sets target to that error value.
This is a thin wrapper around the standard library errors.As, provided so go-service code can depend on a stable import path.
func Is ¶ added in v2.14.0
Is reports whether any error in err's chain matches target.
This is a thin wrapper around the standard library errors.Is.
func Join ¶ added in v2.14.0
Join returns an error that wraps the given errors.
This is a thin wrapper around the standard library errors.Join. A nil error in errs is ignored. If all errors are nil, Join returns nil.
func New ¶ added in v2.14.0
New returns an error that formats as the given text.
This is a thin wrapper around the standard library errors.New. It is typically used to define sentinel errors for comparisons with Is.
func Prefix ¶
Prefix wraps err with a component prefix while preserving it for unwrapping.
It formats the returned error using:
fmt.Errorf("%v: %w", prefix, err)
If err is nil, Prefix returns nil. This makes it convenient to use in return statements without additional nil checks.
Example:
return errors.Prefix("database", err)
Types ¶
This section is empty.