Documentation
¶
Overview ¶
Package errutil 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) All() Errors
- func (b *Bundle) Cap() int
- func (b *Bundle) Error(prefix string) string
- func (b *Bundle) First() error
- func (b *Bundle) Get(index int) error
- func (b *Bundle) IsEmpty() bool
- 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 executes functions in chain and if one of them returns an error, this function stops the chain execution and returns that error
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 bundle of errors
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 (*Bundle) Add ¶
Add adds new error to slice
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()) fmt.Printf("Last: %v\n", errs.Last())
Output: First: Error 1 Last: Error 3
func (*Bundle) All ¶
All returns all errors in slice
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: %v\n", errs.All())
Output: Errors: [Error 1 Error 2 Error 3]
func (*Bundle) Cap ¶
Cap returns maximum bundle capacity
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 text of all 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%s\n", errs.Error(" - "))
Output: Errors: - Error 1 - Error 2 - Error 3
func (*Bundle) First ¶
First returns the first error in 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("First: %v\n", errs.First())
Output: First: Error 1
func (*Bundle) Get ¶
Get returns error by it index
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 bundle is empty
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) Last ¶
Last returns the last error in 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("Last: %v\n", errs.Last())
Output: Last: Error 3
type Errors ¶
type Errors []error
Errors is a slice with 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) First ¶
Last returns the first error from the slice
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 error with given index
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 slice is empty
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