errors

package
v1.60.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package errors provides a set of utilities for working with errors.

Errors created through this package are prefixed with where they came from, so that an error message alone is enough to locate its source:

var errs = errors.FromPackage()

func Run() error {
	return errs.New("failed to wait for caches to sync")
	// "agones.dev/agones/pkg/gameservers: failed to wait for caches to sync"
}

or, scoped to a struct:

type Controller struct {
	errs *errors.Errors
}

func NewController() *Controller {
	c := &Controller{}
	c.errs = errors.FromStruct(c)
	return c
}

func (c *Controller) Run() error {
	return c.errs.New("failed to wait for caches to sync")
	// "agones.dev/agones/pkg/gameservers.Controller: failed to wait for caches to sync"
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

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

Errors creates errors prefixed with their origin, in the form "package: msg", or "package.Struct: msg" when created from a struct. Create one with FromPackage or FromStruct.

The zero value is usable, and produces errors without a prefix.

func FromPackage

func FromPackage() *Errors

FromPackage returns an Errors for the package it is called from, determined by inspecting the calling function at runtime. It is intended to be assigned to a package level variable:

var errs = errors.FromPackage()

func FromStruct

func FromStruct[T any](_ *T) *Errors

FromStruct returns an Errors for the type T, recording both the struct name and the package it is declared in. It takes a pointer so that T is inferred from the call site:

c := &Controller{}
c.errs = errors.FromStruct(c)

Only the argument's type is used, never its value, so a typed nil pointer works as well as a populated one.

If T is an unnamed type, such as a pointer to a builtin or to an anonymous struct, this falls back to the package FromStruct was called from, with no struct name.

func (*Errors) Errorf

func (e *Errors) Errorf(format string, a ...any) error

Errorf returns an error formatted per fmt.Errorf, prefixed as described on Errors. A %w verb in format wraps as usual.

func (*Errors) New

func (e *Errors) New(msg string) error

New returns an error with msg, prefixed as described on Errors.

func (*Errors) Wrap

func (e *Errors) Wrap(err error, msg string) error

Wrap returns an error annotating err with msg, prefixed as described on Errors. The returned error wraps err, so errors.Is and errors.As traverse it.

Wrap returns nil if err is nil.

Jump to

Keyboard shortcuts

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