go-multierror
A Go (golang) package for representing a list of errors as a single error.
Top Related Projects
Quick Overview
go-multierror is a Go library that provides a way to represent and handle multiple errors as a single error. It allows developers to accumulate errors and treat them as a single entity, which is particularly useful in scenarios where multiple operations can fail independently.
Pros
- Simplifies error handling in complex operations
- Provides a clean API for accumulating and accessing multiple errors
- Supports custom formatting of error messages
- Compatible with standard Go error interfaces
Cons
- May encourage less granular error handling in some cases
- Potential overhead for simple error scenarios
- Limited built-in error categorization or prioritization
Code Examples
Example 1: Creating and appending errors
import "github.com/hashicorp/go-multierror"
var result error
if err := operation1(); err != nil {
result = multierror.Append(result, err)
}
if err := operation2(); err != nil {
result = multierror.Append(result, err)
}
if result != nil {
// Handle multiple errors
fmt.Println(result.Error())
}
Example 2: Using a custom error format
import "github.com/hashicorp/go-multierror"
errors := []error{
errors.New("error 1"),
errors.New("error 2"),
}
err := multierror.Append(nil, errors...)
err.ErrorFormat = func([]error) string {
return "Multiple errors occurred"
}
fmt.Println(err.Error()) // Output: Multiple errors occurred
Example 3: Flattening nested multierrors
import "github.com/hashicorp/go-multierror"
err1 := multierror.Append(nil, errors.New("error 1"), errors.New("error 2"))
err2 := multierror.Append(nil, errors.New("error 3"), err1)
flat := multierror.Flatten(err2)
fmt.Println(len(flat.Errors)) // Output: 3
Getting Started
To use go-multierror in your Go project:
-
Install the package:
go get github.com/hashicorp/go-multierror -
Import the package in your Go code:
import "github.com/hashicorp/go-multierror" -
Start using the multierror functionality:
var result error result = multierror.Append(result, errors.New("error 1")) result = multierror.Append(result, errors.New("error 2")) if result != nil { fmt.Println(result.Error()) }
Competitor Comparisons
errors with stacktraces for go
Pros of errors
- More comprehensive error handling with stack traces and error wrapping
- Provides additional utility functions for error manipulation and inspection
- Supports custom error types with additional context
Cons of errors
- Slightly more complex API compared to go-multierror
- May have a steeper learning curve for developers new to advanced error handling
- Potentially higher memory usage due to stack trace storage
Code Comparison
errors:
err := errors.New("something went wrong")
err = errors.Wrap(err, "additional context")
fmt.Printf("%+v\n", err)
go-multierror:
var result error
result = multierror.Append(result, errors.New("error 1"))
result = multierror.Append(result, errors.New("error 2"))
fmt.Println(result)
The errors package offers more detailed error information and context, while go-multierror focuses on aggregating multiple errors into a single error value. errors provides stack traces and error wrapping, which can be beneficial for debugging and error analysis. go-multierror, on the other hand, offers a simpler API for combining multiple errors, which can be useful in scenarios where error aggregation is the primary concern.
Combine one or more Go errors together
Pros of multierr
- More lightweight and focused solely on error handling
- Provides additional utility functions like Append, Combine, and Errors
- Better performance in certain scenarios, especially when combining multiple errors
Cons of multierr
- Less feature-rich compared to go-multierror (e.g., no built-in formatting options)
- May require additional code for custom error formatting
Code Comparison
multierr:
err1 := errors.New("error 1")
err2 := errors.New("error 2")
merr := multierr.Combine(err1, err2)
go-multierror:
var merr *multierror.Error
multierror.Append(&merr, errors.New("error 1"))
multierror.Append(&merr, errors.New("error 2"))
Both libraries provide similar functionality for combining multiple errors, but multierr offers a more concise API. go-multierror requires creating a pointer to a multierror.Error struct and using the Append function, while multierr allows direct combination of errors using the Combine function.
The choice between these libraries depends on specific project requirements, such as performance needs, desired API simplicity, and additional features like custom formatting options.
A comprehensive error handling library for Go
Pros of errorx
- More comprehensive error handling with rich context and type hierarchy
- Built-in error types for common scenarios (e.g., timeout, not found)
- Supports error wrapping and unwrapping with additional metadata
Cons of errorx
- Steeper learning curve due to more complex API
- Potentially higher memory usage for storing additional error context
- May be overkill for simpler projects with basic error handling needs
Code Comparison
errorx:
err := errorx.Decorate(originalError, "additional context")
if errorx.IsOfType(err, errorx.NotFound) {
// Handle not found error
}
go-multierror:
var result error
result = multierror.Append(result, err1)
result = multierror.Append(result, err2)
Summary
errorx offers a more feature-rich error handling solution with built-in error types and extensive context support, while go-multierror focuses on combining multiple errors into a single error value. errorx may be better suited for larger projects requiring detailed error information, while go-multierror provides a simpler approach for aggregating errors in concurrent operations or validation scenarios.
Error handling library with readable stack traces and flexible formatting support 🎆
Pros of eris
- Provides more detailed error information, including stack traces and root cause analysis
- Offers a wider range of error wrapping and formatting options
- Includes built-in support for error classification and custom error types
Cons of eris
- May have a steeper learning curve due to its more extensive feature set
- Potentially higher memory usage when capturing stack traces for all errors
Code Comparison
eris:
err := eris.New("base error")
wrappedErr := eris.Wrap(err, "additional context")
fmt.Println(eris.ToString(wrappedErr, true))
go-multierror:
var result error
result = multierror.Append(result, errors.New("error 1"))
result = multierror.Append(result, errors.New("error 2"))
fmt.Println(result.Error())
eris focuses on providing rich error context and stack traces, while go-multierror primarily deals with aggregating multiple errors. eris offers more comprehensive error handling capabilities, but may require more setup and understanding to use effectively. go-multierror is simpler and more straightforward for basic error aggregation needs, but lacks advanced features like stack trace capture and error classification.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
go-multierror
Note: As of Go 1.20, the standard library provides
errors.Joinwhich offers similar functionality for combining multiple errors. For new projects, we recommend usingerrors.Joinfrom the standard library. This package provides additional features like custom formatting, theGrouppattern for concurrent error collection, and utility functions (Append,Flatten,Prefix).
Migrating to errors.Join
Basic error aggregation:
// Before (go-multierror)
var result error
result = multierror.Append(result, err1)
result = multierror.Append(result, err2)
return result
// After (stdlib)
var errs []error
if err1 != nil {
errs = append(errs, err1)
}
if err2 != nil {
errs = append(errs, err2)
}
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
Custom formatting:
// go-multierror allows custom formatting
// With stdlib, wrap the joined error with a custom type if needed
type FormattedError struct {
errs []error
}
func (e *FormattedError) Error() string {
// Your custom format here
return fmt.Sprintf("multiple errors: %v", e.errs)
}
func (e *FormattedError) Unwrap() []error {
return e.errs
}
Concurrent error collection (Group):
// golang.org/x/sync/errgroup returns only the first error:
import "golang.org/x/sync/errgroup"
g := new(errgroup.Group)
g.Go(func() error { return task1() })
g.Go(func() error { return task2() })
err := g.Wait() // Returns first error only
// To collect ALL errors like multierror.Group does, use a mutex:
type ErrorCollector struct {
mu sync.Mutex
errs []error
}
func (ec *ErrorCollector) Add(err error) {
if err != nil {
ec.mu.Lock()
ec.errs = append(ec.errs, err)
ec.mu.Unlock()
}
}
func (ec *ErrorCollector) Err() error {
ec.mu.Lock()
defer ec.mu.Unlock()
if len(ec.errs) == 0 {
return nil
}
return errors.Join(ec.errs...)
}
go-multierror is a package for Go that provides a mechanism for
representing a list of error values as a single error.
This allows a function in Go to return an error that might actually
be a list of errors. If the caller knows this, they can unwrap the
list and access the errors. If the caller doesn't know, the error
formats to a nice human-readable format.
go-multierror is compatible with the Go standard library
errors package, supporting the
As, Is, and Unwrap functions for error introspection. Note that Unwrap
returns errors one at a time via chaining, unlike errors.Join which
implements the newer Unwrap() []error signature (Go 1.20+).
Installation and Docs
Install using go get github.com/hashicorp/go-multierror.
Full documentation is available at https://pkg.go.dev/github.com/hashicorp/go-multierror
Requires go version 1.13 or newer
go-multierror requires go version 1.13 or newer. Go 1.13 introduced
error wrapping, which
this library takes advantage of.
If you need to use an earlier version of go, you can use the v1.0.0 tag, which doesn't rely on features in go 1.13.
If you see compile errors that look like the below, it's likely that you're on an older version of go:
/go/src/github.com/hashicorp/go-multierror/multierror.go:112:9: undefined: errors.As
/go/src/github.com/hashicorp/go-multierror/multierror.go:117:9: undefined: errors.Is
Usage
go-multierror is easy to use and purposely built to be unobtrusive in existing Go applications/libraries that may not be aware of it.
Building a list of errors
The Append function is used to create a list of errors. This function
behaves a lot like the Go built-in append function: it doesn't matter
if the first argument is nil, a multierror.Error, or any other error,
the function behaves as you would expect.
var result error
if err := step1(); err != nil {
result = multierror.Append(result, err)
}
if err := step2(); err != nil {
result = multierror.Append(result, err)
}
return result
Customizing the formatting of the errors
By specifying a custom ErrorFormat, you can customize the format
of the Error() string function:
var result *multierror.Error
// ... accumulate errors here, maybe using Append
if result != nil {
result.ErrorFormat = func([]error) string {
return "errors!"
}
}
Accessing the list of errors
multierror.Error implements error so if the caller doesn't know about
multierror, it will work just fine. But if you're aware a multierror might
be returned, you can use type switches to access the list of errors:
if err := something(); err != nil {
if merr, ok := err.(*multierror.Error); ok {
// Use merr.Errors
}
}
You can also use the standard errors.Unwrap
function. This will continue to unwrap into subsequent errors until none exist.
Extracting an error
The standard library errors.As
function can be used directly with a multierror to extract a specific error:
// Assume err is a multierror value
err := somefunc()
// We want to know if "err" has a "RichErrorType" in it and extract it.
var errRich RichErrorType
if errors.As(err, &errRich) {
// It has it, and now errRich is populated.
}
Checking for an exact error value
Some errors are returned as exact errors such as the ErrNotExist
error in the os package. You can check if this error is present by using
the standard errors.Is function.
// Assume err is a multierror value
err := somefunc()
if errors.Is(err, os.ErrNotExist) {
// err contains os.ErrNotExist
}
Returning a multierror only if there are errors
If you build a multierror.Error, you can use the ErrorOrNil function
to return an error implementation only if there are errors to return:
var result *multierror.Error
// ... accumulate errors here
// Return the `error` only if errors were added to the multierror, otherwise
// return nil since there are no errors.
return result.ErrorOrNil()
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot