multierr

package module
v0.0.0-...-61c43d1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 6 Imported by: 1

README

multierr

Overview

multierr is a Go module for aggregating multiple errors into a single error. It is fully compatible with the errors package within the Go standard library, including the errors.Is and errors.As functions to provide a standardized approach for introspecting error values.

Usage

Creating an error

The multierr.New function is used to create a new multierr.Error from a list of errors.

return multierr.New(
    errors.New("first error"),
    errors.New("second error"),
)
Aggregating errors

The multierr.Append function is used to aggregate multiple errors into a single error. It has similar semantics to the built-in append function:

var errs error

err := step1()
if err != nil {
    errs = multierr.Append(errs, err)
}

err = step2()
if err != nil {
    errs = multierr.Append(errs, err)
}

return errs
Checking for an error

The errors.is function can be used directly with a multierr.Error to check for a specific error:

// Assume that err is a multierr.Error
err := someFunc()
if err != nil {
    if errors.Is(err, SomeError) {
        // err contains SomeError
    }
}
Extracting an error

The errors.As function can be used directly with a multierr.Error to extract a specific error:

// Assume that err is a multierr.Error
err := someFunc()
if err != nil {
    var someError *SomeError
    if errors.As(err, &someError) {
        // err contains SomeError and populates someError
    }
}

Documentation

Documentation for multierr can be found here.

Documentation

Overview

Package multierr is a Go package for aggregating multiple errors into a single error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

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

Error is an error type that is used to aggregate multiple errors into a single error.

func Append

func Append(err error, errs ...error) *Error

Append returns an Error by appending errs onto err.

If err is not an Error then it will be turned into one. Any nil errors contained within errs are removed.

func New

func New(errs ...error) *Error

New returns a new Error that contains errs. Any nil errors contained within errs are removed.

func (*Error) Error

func (e *Error) Error() string

func (*Error) GoString

func (e *Error) GoString() string

func (*Error) Len

func (e *Error) Len() int

Len returns the number of errors that this Error wraps.

func (*Error) Unwrap

func (e *Error) Unwrap() []error

Unwrap returns the list of errors that this Error wraps.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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