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 ¶
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 ¶
Errorf returns an error formatted per fmt.Errorf, prefixed as described on Errors. A %w verb in format wraps as usual.