Help us shape the future of Go! Take the 2026 Go Developer Survey

multierrors

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: BSD-2-Clause Imports: 3 Imported by: 0

README

multierrors

Golang multi errors

Usage

Import
import "github.com/shiolier/multierrors"
Usage
err1 := errors.New("error1")
err2 := errors.New("error2")
// New
errs := multierrors.New(err1, err2)

// Add
errs.Add(errors.New("error3"))

// Delimiter
errs.SetDelimiter(", ")
// Error()
fmt.Fprintln(os.Stderr, errs.Error())
// => error1, error2, error3

err4 := errors.New("error4")
err4wrap := fmt.Errorf("error4wrap: %w", err4)
errs.Add(err4wrap)
// errors.Is
fmt.Println(errors.Is(errs, err4))
// => true

/*
type SampleError struct {
    Msg string
}
func (e SampleError) Error() string {
    return e.Msg
}
*/
err5 := SampleError{Msg: "error5"}
err5wrap := fmt.Errorf("error5wrap: %w", err5)
errs.Add(err5wrap)
smplerr := SampleError{}
// errors.As
fmt.Println(errors.As(errs, &smplerr), smplerr.Msg)
// => true error5

// Each
errs.Each(func(idx int, err error) {
    fmt.Printf("index=%d, error=%v\n", idx, err)
})
/*
=>
    index=0, error=error1
    index=1, error=error2
    index=2, error=error3
    index=3, error=error4wrap: error4
    index=4, error=error5wrap: error5
*/

// Filter
newerrs := errs.Filter(func(idx int, err error) bool {
    return idx%2 == 0
})
fmt.Println(newerrs.Length())
// => 3
fmt.Fprintln(os.Stderr, newerrs.Error())
// => error1, error3, error5wrap: error5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDelimiter

func DefaultDelimiter() string

DefaultDelimiter is the getter of default delimiter

func SetDefaultDelimiter

func SetDefaultDelimiter(s string)

SetDefaultDelimiter is the setter of default delimiter

Types

type MultiErrors

type MultiErrors struct {
	// contains filtered or unexported fields
}

MultiErrors has multiple errors and can be used as an error

func New

func New(errs ...error) *MultiErrors

New returns MultiErrors

func (*MultiErrors) Add

func (e *MultiErrors) Add(errs ...error)

Add error

func (*MultiErrors) As

func (e *MultiErrors) As(target interface{}) bool

As to support errors.As

func (*MultiErrors) Delimiter

func (e *MultiErrors) Delimiter() string

Delimiter is the getter of delimiter

func (*MultiErrors) Each

func (e *MultiErrors) Each(fn func(int, error))

Each

func (*MultiErrors) Error

func (e *MultiErrors) Error() string

Error is the implementation of interface error

func (*MultiErrors) Errors

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

Errors returns a copy of the internal error slice

func (*MultiErrors) Filter

func (e *MultiErrors) Filter(fn func(int, error) bool) *MultiErrors

Filter returns new MultiErrors created with multiple errors that pass the condition

func (*MultiErrors) Is

func (e *MultiErrors) Is(target error) bool

Is to support errors.Is

func (*MultiErrors) Length

func (e *MultiErrors) Length() int

Length of the internal error slice

func (*MultiErrors) SetDelimiter

func (e *MultiErrors) SetDelimiter(del string)

SetDelimiter is the setter of delimiter

Jump to

Keyboard shortcuts

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