Skip to content

A Go module for aggregating multiple errors into a single error.

License

Notifications You must be signed in to change notification settings

jordanhasgul/multierr

Repository files navigation

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 a multierr

As per the Go proverb, the zero value of the multierr.Error is useful, and you can simply create a multierr.Error as follows and begin using it:

var err multierr.Error

However, if you would like to construct a multierr.Error from a pre-existing slice of errors, you can use the multierr.New function instead:

err := multierr.New(
    errors.New("first error"),
    errors.New("second error"),
)

Aggregating errors with multierr

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

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 from a multierr

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.

About

A Go module for aggregating multiple errors into a single error.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages