Documentation
¶
Overview ¶
Package errors provides methods for working with errors
Index ¶
- func As(err error, target any) bool
- func Chain(funcs ...func() error) error
- func Is(err, target error) bool
- func Join(errs ...error) error
- func New(text string) error
- func Unwrap(err error) error
- type Bundle
- func (b *Bundle) Add(errs ...any) *Bundle
- func (b *Bundle) Addf(format string, a ...any) *Bundle
- func (b *Bundle) All() Errors
- func (b *Bundle) Cap() int
- func (b *Bundle) Error() string
- func (b *Bundle) ErrorWithPrefix(prefix string) string
- func (b *Bundle) First() error
- func (b *Bundle) Get(index int) error
- func (b *Bundle) IsEmpty() bool
- func (b *Bundle) Join() error
- func (b *Bundle) Last() error
- func (b *Bundle) Num() int
- func (b *Bundle) Reset()
- type Errors
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
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 ¶
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
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 ¶
NewBundle creates a new empty Bundle with an optional maximum capacity. When capacity is exceeded, the oldest errors are dropped.
func ToBundle ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
type Errors ¶
type Errors []error
Errors is a slice of errors
func (Errors) Error ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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