errutil

package
v12.46.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: Apache-2.0 Imports: 1 Imported by: 1

Documentation

Overview

Package errutil provides methods for working with errors

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

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

Chain executes functions in chain and if one of them return error this function stop chain execution and return this 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 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 NewErrors

func NewErrors(capacity ...int) *Errors

NewErrors creates new struct

func (*Errors) Add

func (e *Errors) Add(errs ...interface{}) *Errors

Add adds new error to slice

func (*Errors) All

func (e *Errors) All() []error

All returns all errors in slice

func (*Errors) Cap

func (e *Errors) Cap() int

Cap returns max capacity

func (*Errors) Error

func (e *Errors) Error() string

Error returns text of all errors

func (*Errors) HasErrors

func (e *Errors) HasErrors() bool

HasErrors checks if slice contains errors

func (*Errors) Last

func (e *Errors) Last() error

Last returns the last error

func (*Errors) Num

func (e *Errors) Num() int

Num returns number of errors

func (*Errors) Reset

func (e *Errors) Reset()

Reset resets Errors instance to be empty

Jump to

Keyboard shortcuts

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