errors

package
v14.1.7 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package errors provides methods for working with errors

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

func Chain

func Chain(funcs ...func() error) error

Chain calls each function in order and stops at the first non-nil error, returning it

Example
f1 := func() error { return nil }
f2 := func() error { return nil }
f3 := func() error { return fmt.Errorf("Error 3") }
f4 := func() error { return fmt.Errorf("Error 4") }

err := Chain(f1, f2, f3, f4)

fmt.Println(err.Error())
Output:
Error 3

func Is

func Is(err, target error) bool

Is reports whether any error in err's tree matches target

func Join

func Join(errs ...error) error

Join returns an error that wraps the given errors

func New

func New(text string) error

New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

Types

type Bundle

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

Bundle is a capacity-bounded, nil-safe collection of errors. Its zero value is ready to use without initialization.

Example
f1 := func() error { return nil }
f2 := func() error { return nil }
f3 := func() error { return fmt.Errorf("Error 3") }
f4 := func() error { return fmt.Errorf("Error 4") }

// An Bundle needs no initialization
var myErrs Bundle

myErrs.Add(f1())

// Using NewBundle you can create Bundle instance with limited capacity
errs := NewBundle(10)

errs.Add(f1())
errs.Add(f2())
errs.Add(f3())
errs.Add(f4())

fmt.Printf("Last error text: %v\n", errs.Last().Error())
fmt.Printf("Number of errors: %d\n", errs.Num())
fmt.Printf("Capacity: %d\n", errs.Cap())
fmt.Printf("Has errors: %t\n", !errs.IsEmpty())
Output:
Last error text: Error 4
Number of errors: 2
Capacity: 10
Has errors: true

func NewBundle

func NewBundle(capacity ...int) *Bundle

NewBundle creates a new empty Bundle with an optional maximum capacity. When capacity is exceeded, the oldest errors are dropped.

func ToBundle

func ToBundle(errs Errors) *Bundle

ToBundle wraps an existing Errors slice into a Bundle

Example
e := []error{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

errs := ToBundle(e)

fmt.Printf("Last error text: %v\n", errs.Last().Error())
fmt.Printf("Number of errors: %d\n", errs.Num())
fmt.Printf("Has errors: %t\n", !errs.IsEmpty())
Output:
Last error text: Error 3
Number of errors: 3
Has errors: true

func (*Bundle) Add

func (b *Bundle) Add(errs ...any) *Bundle

Add appends one or more errors to the bundle. Accepts error, string, []error, []string, Errors, Bundle, and *Bundle values.

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add("Error 2")
errs.Add(`Error 3`)

fmt.Printf("First: %v\n", errs.First())
fmt.Printf("Last: %v\n", errs.Last())
Output:
First: Error 1
Last: Error 3

func (*Bundle) Addf

func (b *Bundle) Addf(format string, a ...any) *Bundle

Addf formats an error using fmt.Errorf and appends it to the bundle

Example
var errs Bundle

errs.Addf("Error %d", 1)
errs.Addf("Error %d", 2)
errs.Addf("Error %d", 3)

fmt.Printf("First: %v\n", errs.First())
fmt.Printf("Last: %v\n", errs.Last())
Output:
First: Error 1
Last: Error 3

func (*Bundle) All

func (b *Bundle) All() Errors

All returns the full slice of collected errors

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Errors:\n%v\n", errs.All())
Output:
Errors:
Error 1
Error 2
Error 3

func (*Bundle) Cap

func (b *Bundle) Cap() int

Cap returns the maximum number of errors the bundle will retain, or 0 if unbounded

Example
errs := NewBundle(2)

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Errors cap: %d\n", errs.Cap())
fmt.Printf("First: %v\n", errs.First())
fmt.Printf("Last: %v\n", errs.Last())
Output:
Errors cap: 2
First: Error 2
Last: Error 3

func (*Bundle) Error

func (b *Bundle) Error() string

Error returns all error messages joined by newlines

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Errors:\n%s\n", errs.Error())
Output:
Errors:
Error 1
Error 2
Error 3

func (*Bundle) ErrorWithPrefix

func (b *Bundle) ErrorWithPrefix(prefix string) string

ErrorWithPrefix returns all error messages joined by newlines, each prefixed with prefix

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Errors:\n%s\n", errs.ErrorWithPrefix(" - "))
Output:
Errors:
 - Error 1
 - Error 2
 - Error 3

func (*Bundle) First

func (b *Bundle) First() error

First returns the first error in the bundle, or nil if empty

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("First: %v\n", errs.First())
Output:
First: Error 1

func (*Bundle) Get

func (b *Bundle) Get(index int) error

Get returns the error at the given index, or nil if the index is out of range

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Index 1: %v\n", errs.Get(1))
fmt.Printf("Index 99: %v\n", errs.Get(99))
Output:
Index 1: Error 2
Index 99: <nil>

func (*Bundle) IsEmpty

func (b *Bundle) IsEmpty() bool

IsEmpty returns true if the bundle contains no errors

Example
var errs Bundle

fmt.Printf("Has errors: %t\n", !errs.IsEmpty())

errs.Add(fmt.Errorf("Error"))

fmt.Printf("Has errors: %t\n", !errs.IsEmpty())
Output:
Has errors: false
Has errors: true

func (*Bundle) Join

func (b *Bundle) Join() error

Join returns an error that wraps all errors in the Bundle

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Errors:\n%v\n", errs.Join())
Output:
Errors:
Error 1
Error 2
Error 3

func (*Bundle) Last

func (b *Bundle) Last() error

Last returns the last error in the bundle, or nil if empty

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Last: %v\n", errs.Last())
Output:
Last: Error 3

func (*Bundle) Num

func (b *Bundle) Num() int

Num returns the number of errors currently in the bundle

Example
var errs Bundle

errs.Add(fmt.Errorf("Error 1"))
errs.Add(fmt.Errorf("Error 2"))
errs.Add(fmt.Errorf("Error 3"))

fmt.Printf("Errors num: %d\n", errs.Num())
Output:
Errors num: 3

func (*Bundle) Reset

func (b *Bundle) Reset()

Reset removes all errors from the bundle without changing its capacity

Example
var errs Bundle

errs.Add(fmt.Errorf("Error"))
errs.Reset()

fmt.Printf("Has errors: %t\n", !errs.IsEmpty())
Output:
Has errors: false

type Errors

type Errors []error

Errors is a slice of errors

func (Errors) Error

func (e Errors) Error() string

Error returns combined text of all errors in the slice

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("Errors:\n%s\n", errs.Error())
Output:
Errors:
Error 1
Error 2
Error 3

func (Errors) ErrorWithPrefix

func (e Errors) ErrorWithPrefix(prefix string) string

ErrorWithPrefix returns combined text of all errors in the slice with prefix before every error

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("Errors:\n%s\n", errs.ErrorWithPrefix(" - "))
Output:
Errors:
 - Error 1
 - Error 2
 - Error 3

func (Errors) First

func (e Errors) First() error

First returns the first error in the slice, or nil if empty

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("First: %v\n", errs.First())
Output:
First: Error 1

func (Errors) Get

func (e Errors) Get(index int) error

Get returns the error at the given index, or nil if the index is out of range

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("Index 1: %v\n", errs.Get(1))
fmt.Printf("Index 99: %v\n", errs.Get(99))
Output:
Index 1: Error 2
Index 99: <nil>

func (Errors) IsEmpty

func (e Errors) IsEmpty() bool

IsEmpty returns true if the slice contains no errors

Example
var errs Errors

fmt.Printf("Has errors: %t\n", !errs.IsEmpty())

errs = Errors{fmt.Errorf("Error 1")}

fmt.Printf("Has errors: %t\n", !errs.IsEmpty())
Output:
Has errors: false
Has errors: true

func (Errors) Join

func (e Errors) Join() error

Join returns an error that wraps all errors in the Errors slice

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("Errors:\n%v\n", errs.Join())
Output:
Errors:
Error 1
Error 2
Error 3

func (Errors) Last

func (e Errors) Last() error

Last returns the last error in the slice, or nil if empty

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("Last: %v\n", errs.Last())
Output:
Last: Error 3

func (Errors) Num

func (e Errors) Num() int

Num returns the number of errors in the slice

Example
errs := Errors{
	fmt.Errorf("Error 1"),
	fmt.Errorf("Error 2"),
	fmt.Errorf("Error 3"),
}

fmt.Printf("Errors num: %d\n", errs.Num())
Output:
Errors num: 3

Jump to

Keyboard shortcuts

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