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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages