errors

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errors adds typed error semantics on top of the stdlib errors package: a stable machine-readable Code, an optional HTTP status hint, and Wrap/Is/As support. Consumers who just want stdlib behaviour can keep using errors — this package is opt-in for structured errors.

Typical usage in a service:

ErrNotFound  = errors.New(errors.CodeNotFound, "not found")
ErrConflict  = errors.New(errors.CodeConflict, "conflict")

func GetUser(id string) (*User, error) {
    u, err := repo.Find(id)
    if err != nil {
        return nil, errors.Wrap(err, ErrNotFound, "user %q", id)
    }
    return u, nil
}

// In an HTTP handler:
if err := svc.Do(); err != nil {
    status := errors.HTTPStatus(err)          // 404 for ErrNotFound
    code := errors.CodeOf(err)                // "not_found"
    // ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As is a passthrough to errors.As.

func HTTPStatus

func HTTPStatus(err error) int

HTTPStatus maps err's code to a suggested HTTP status. Unknown codes fall through to 500. Consumers who want app-specific mappings should bring their own function; this is a reasonable default only.

func Is

func Is(err, target error) bool

Is is a passthrough to errors.Is for consumers who use this package by default and want to avoid two errors imports.

func Join

func Join(errs ...error) error

Join is a passthrough to errors.Join.

func Unwrap

func Unwrap(err error) error

Unwrap is a passthrough to errors.Unwrap.

Types

type Code

type Code string

Code is a stable machine-readable identifier. Consumers define their own codes as constants; hex ships the common ones as a starting point.

const (
	CodeInternal        Code = "internal"
	CodeInvalidArgument Code = "invalid_argument"
	CodeNotFound        Code = "not_found"
	CodeConflict        Code = "conflict"
	CodeUnauthorized    Code = "unauthorized"
	CodeForbidden       Code = "forbidden"
	CodeRateLimited     Code = "rate_limited"
	CodeUnavailable     Code = "unavailable"
	CodeTimeout         Code = "timeout"
)

Common code constants. Consumers may extend with their own; these cover the most frequent HTTP-mappable failures.

func CodeOf

func CodeOf(err error) Code

CodeOf returns err's code if it (or any wrapped error) is an *Error; otherwise CodeInternal.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is the typed error type. It carries a Code, a message, and an optional wrapped cause.

func New

func New(code Code, msg string) *Error

New returns a fresh Error with the given code and message.

func Newf

func Newf(code Code, format string, args ...any) *Error

Newf is New with fmt.Sprintf-style formatting.

func Wrap

func Wrap(cause error, target *Error, format string, args ...any) *Error

Wrap attaches cause to a new Error carrying msg. The result satisfies errors.Is / errors.As against both the target sentinel and the cause. If target is nil, cause's own code is preserved when possible.

func (*Error) Code

func (e *Error) Code() Code

Code returns the Error's code, or CodeInternal if unset.

func (*Error) Error

func (e *Error) Error() string

Error returns the fully composed message. If a cause is attached, its message follows a colon.

func (*Error) Is

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

Is matches on Code equality. This makes sentinel comparisons work:

errors.Is(err, ErrNotFound)  // true when err has code "not_found"

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped cause so errors.Is / errors.As walk the chain.

Jump to

Keyboard shortcuts

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