errors

package
v13.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: Apache-2.0 Imports: 2 Imported by: 6

Documentation

Overview

Package errutil 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 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

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 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 NewBundle

func NewBundle(capacity ...int) *Bundle

NewBundle creates new errors bundle

func ToBundle

func ToBundle(errs Errors) *Bundle

ToBundle wraps slice of errors into Bundle

func (*Bundle) Add

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

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

func (b *Bundle) All() Errors

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

func (b *Bundle) Cap() int

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

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

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

func (b *Bundle) First() error

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

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

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

func (b *Bundle) IsEmpty() bool

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

func (b *Bundle) Last() error

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

func (*Bundle) Num

func (b *Bundle) Num() int

Num returns number of 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 num: %d\n", errs.Num())
Output:

Errors num: 3

func (*Bundle) Reset

func (b *Bundle) Reset()

Reset resets instance to be empty

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 with errors

func (Errors) Error

func (e Errors) Error(prefix string) 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) First

func (e Errors) First() error

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

func (e Errors) Get(index int) error

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

func (e Errors) IsEmpty() bool

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

func (Errors) Last

func (e Errors) Last() error

Last returns the last error from the slice

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 size of 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