errors

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package errors provides error handling primitives for Go 1.21+.

This is a modern reimplementation of error handling, designed for Go 1.21+. It leverages the standard library's errors package (errors.Is, errors.As, errors.Join, fmt.Errorf with %w) while providing error codes, stack traces, and a registration system for API error responses.

Key features:

  • Error codes with HTTP status mapping
  • Stack traces for debugging
  • Unwrap support for error chain traversal
  • Compatible with errors.Is and errors.As

Basic usage:

err := errors.New("something went wrong")
err = errors.Wrap(err, "context added")
err = errors.WithCode(ErrNotFound, "user %d not found", userId)

Checking error codes:

if errors.IsCode(err, ErrNotFound) {
    // handle not found
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// Is reports whether any error in err's tree matches target.
	Is = errors.Is
	// As finds the first error in err's tree that matches target.
	As = errors.As
	// Join returns an error that wraps the given errors.
	Join = errors.Join
	// Unwrap returns the result of calling the Unwrap method on err.
	Unwrap = errors.Unwrap
)

Re-export standard library functions for convenience

Functions

func Cause

func Cause(err error) error

Cause returns the root cause of the error, traversing the error chain. This is similar to errors.Unwrap but unwraps the entire chain.

func Errorf

func Errorf(format string, args ...any) error

Errorf formats according to a format specifier and returns an error with a stack trace.

func FormatStack

func FormatStack(stack []uintptr) string

FormatStack returns a formatted stack trace as a string.

func GetCode

func GetCode(err error) int

GetCode extracts the error code from an error. Returns 0 if no code is found.

func GetStackTrace

func GetStackTrace(err error) []uintptr

GetStackTrace extracts stack trace from an error if available.

func HTTPStatus

func HTTPStatus(code int) int

HTTPStatus returns the HTTP status for an error code. Returns 500 if code is not registered.

func IsCode

func IsCode(err error, code int) bool

IsCode reports whether any error in err's chain has the given code.

func MustRegister

func MustRegister(code int, httpStatus int, message string)

MustRegister is like Register but allows overwriting existing codes.

func New

func New(message string) error

New returns an error with the supplied message and a stack trace.

func Register

func Register(code int, httpStatus int, message string)

Register registers an error code with its HTTP status and message. Panics if the code is 0 or already registered.

func WithCode

func WithCode(code int, format string, args ...any) error

WithCode creates a new error with the given code.

func WithMessage

func WithMessage(err error, message string) error

WithMessage annotates err with a message (no stack trace). If err is nil, WithMessage returns nil.

func WithMessagef

func WithMessagef(err error, format string, args ...any) error

WithMessagef annotates err with a formatted message (no stack trace). If err is nil, WithMessagef returns nil.

func Wrap

func Wrap(err error, message string) error

Wrap annotates err with a message and stack trace. If err is nil, Wrap returns nil.

func WrapCode

func WrapCode(err error, code int, format string, args ...any) error

WrapCode wraps an error with a code. If err is nil, returns nil.

func Wrapf

func Wrapf(err error, format string, args ...any) error

Wrapf annotates err with a formatted message and stack trace. If err is nil, Wrapf returns nil.

Types

type CodedError

type CodedError interface {
	Code() int
}

CodedError is implemented by errors that have a code.

type Coder

type Coder interface {
	// Code returns the error code.
	Code() int
	// HTTPStatus returns the HTTP status code.
	HTTPStatus() int
	// Message returns the user-facing message.
	Message() string
}

Coder defines an interface for error codes.

func Lookup

func Lookup(code int) (Coder, bool)

Lookup retrieves a Coder by code.

func ParseCoder

func ParseCoder(err error) Coder

ParseCoder extracts Coder from an error. Returns unknownCoder if no code is found.

type StackTracer

type StackTracer interface {
	StackTrace() []uintptr
}

StackTracer is implemented by errors that have a stack trace.

Jump to

Keyboard shortcuts

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