errors

package
v2.349.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 2 Imported by: 0

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/AsType/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
  • AsType: generic typed lookup in an error chain
  • 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`, `AsType`, `Is`, `Join`, `New`, and `Prefix`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v2.14.0

func As(err error, target any) bool

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.

See: https://pkg.go.dev/errors#As

func AsType added in v2.303.0

func AsType[T error](err error) (T, bool)

AsType reports whether any error in err's chain matches T, and if so returns the matched value.

This is a thin wrapper around the standard library errors.AsType. It is useful when callers want the matched typed value directly instead of allocating a temporary target variable first.

If no matching error is found, AsType returns the zero value of T and false.

See: https://pkg.go.dev/errors#AsType

func Is added in v2.14.0

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

This is a thin wrapper around the standard library errors.Is.

See: https://pkg.go.dev/errors#Is

func Join added in v2.14.0

func Join(errs ...error) error

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.

See: https://pkg.go.dev/errors#Join

func New added in v2.14.0

func New(text string) error

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.

See: https://pkg.go.dev/errors#New

func Prefix

func Prefix(prefix string, err error) error

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.

Jump to

Keyboard shortcuts

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