donjaime / multierror

Go package for encapsulating multiple errors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

multierror

multierror is a simple Go package for combining multiple errors. This is handy if you are concurrently running operations within a function that returns only a single error.

API

multierror exposes just one type.

multierror.MultiError implements the error interface. It is also an alias of []error. So multierror.MultiError can be appended to.

*multierror.MultiError also exposes an Add(error) method which is slightly nicer than using the mErr = append(mErr, err) idiom.

Example

package main

import (
	"fmt"
	"github.com/donjaime/multierror"
)

func main() {
	// Collect multiple errors together in multierror.Errors
	var errs multierror.MultiError
	// Using append()
	errs = append(errs, fmt.Errorf("Error 1"))
	// Or via an Add() convenience method on a *multierror.MultiError
	(&errs).Add(fmt.Errorf("Error 2"))

	// Output: "2 errors: Error 1; Error 2"
	fmt.Println(errs)

	// Iterate over the individual errors
	for _, err := range errs {
		fmt.Println(err) // Output: "Error 1" and "Error 2"
	}
}

About

Go package for encapsulating multiple errors

License:Other


Languages

Language:Go 100.0%