Documentation ¶
Overview ¶
Package errutil provides methods for working with errors
Deprecated: Use package errors instead
Index ¶
- func Chain(funcs ...func() error) errordeprecated
- type Errors
- func NewErrors(capacity ...int) *Errorsdeprecated
- func Wrap(errs []error) *Errorsdeprecated
- func (e *Errors) Add(errs ...any) *Errorsdeprecated
- func (e *Errors) All() []errordeprecated
- func (e *Errors) Cap() intdeprecated
- func (e *Errors) Error() stringdeprecated
- func (e *Errors) First() errordeprecated
- func (e *Errors) Get(index int) errordeprecated
- func (e *Errors) HasErrors() booldeprecated
- func (e *Errors) Last() errordeprecated
- func (e *Errors) Num() intdeprecated
- func (e *Errors) Reset()deprecated
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain
deprecated
Chain executes functions in chain and if one of them return error this function stop chain execution and return this error
Deprecated: Use package errors instead
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 Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
Errors is struct for handling many errors at once
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 Errors needs no initialization var myErrs Errors myErrs.Add(f1()) // Using NewErrors you can create Errors instance with limited capacity errs := NewErrors(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.HasErrors())
Output: Last error text: Error 4 Number of errors: 2 Capacity: 10 Has errors: true
func Wrap
deprecated
added in
v13.2.0
Wrap wraps slice of errors into Errors struct
Deprecated: Use package errors instead
Example ¶
e := []error{ fmt.Errorf("Error 1"), fmt.Errorf("Error 2"), fmt.Errorf("Error 3"), } errs := Wrap(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.HasErrors())
Output: Last error text: Error 3 Number of errors: 3 Has errors: true
func (*Errors) Add
deprecated
Add adds new error to slice
Deprecated: Use package errors instead
Example ¶
var myErrs Errors myErrs.Add(fmt.Errorf("Error 1")) myErrs.Add(fmt.Errorf("Error 2")) myErrs.Add(fmt.Errorf("Error 3")) fmt.Printf("First: %v\n", myErrs.First()) fmt.Printf("Last: %v\n", myErrs.Last())
Output: First: Error 1 Last: Error 3
func (*Errors) All
deprecated
All returns all errors in slice
Deprecated: Use package errors instead
Example ¶
var myErrs Errors myErrs.Add(fmt.Errorf("Error 1")) myErrs.Add(fmt.Errorf("Error 2")) myErrs.Add(fmt.Errorf("Error 3")) fmt.Printf("Errors: %v\n", myErrs.All())
Output: Errors: [Error 1 Error 2 Error 3]
func (*Errors) Cap
deprecated
Cap returns max capacity
Deprecated: Use package errors instead
Example ¶
myErrs := NewErrors(2) myErrs.Add(fmt.Errorf("Error 1")) myErrs.Add(fmt.Errorf("Error 2")) myErrs.Add(fmt.Errorf("Error 3")) fmt.Printf("Errors cap: %d\n", myErrs.Cap()) fmt.Printf("First: %v\n", myErrs.First()) fmt.Printf("Last: %v\n", myErrs.Last())
Output: Errors cap: 2 First: Error 2 Last: Error 3
func (*Errors) Error
deprecated
Error returns text of all errors
Deprecated: Use package errors instead
Example ¶
var myErrs Errors myErrs.Add(fmt.Errorf("Error 1")) myErrs.Add(fmt.Errorf("Error 2")) myErrs.Add(fmt.Errorf("Error 3")) fmt.Printf("Errors:\n%s\n", myErrs.Error())
Output: Errors: Error 1 Error 2 Error 3
func (*Errors) First
deprecated
func (*Errors) Get
deprecated
Get returns error by it index
Deprecated: Use package errors instead
Example ¶
var myErrs Errors myErrs.Add(fmt.Errorf("Error 1")) myErrs.Add(fmt.Errorf("Error 2")) myErrs.Add(fmt.Errorf("Error 3")) fmt.Printf("Index 1: %v\n", myErrs.Get(1)) fmt.Printf("Index 99: %v\n", myErrs.Get(99))
Output: Index 1: Error 2 Index 99: <nil>
func (*Errors) HasErrors
deprecated
HasErrors checks if slice contains errors
Deprecated: Use package errors instead
Example ¶
var myErrs Errors fmt.Printf("Has errors: %t\n", myErrs.HasErrors()) myErrs.Add(fmt.Errorf("Error")) fmt.Printf("Has errors: %t\n", myErrs.HasErrors())
Output: Has errors: false Has errors: true