https://github.com/hashicorp/go-multierror
A Go (golang) package for representing a list of errors as a single error.
https://github.com/hashicorp/go-multierror
Last synced: 7 months ago
JSON representation
A Go (golang) package for representing a list of errors as a single error.
- Host: GitHub
- URL: https://github.com/hashicorp/go-multierror
- Owner: hashicorp
- License: mpl-2.0
- Created: 2014-12-15T20:12:26.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2025-03-13T12:38:11.000Z (9 months ago)
- Last Synced: 2025-05-05T22:26:57.877Z (7 months ago)
- Language: Go
- Size: 103 KB
- Stars: 2,453
- Watchers: 300
- Forks: 129
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
- go-awesome - go-multierror - 支持错误列表 (开源类库 / 错误处理)
- awesome-go-plus - go-multierror - Go (golang) package for representing a list of errors as a single error.  (Error Handling / Search and Analytic Databases)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. Stars:`2.5K`. (Error Handling / Search and Analytic Databases)
- awesome-go-cn - go-multierror - multierror) (错误处理 / 检索及分析资料库)
- awesome-list - go-multierror
- go-awesome - go-multierror - Support error list (Open source library / Error Handling)
- awesome-go - go-multierror - A Go (golang) package for representing a list of errors as a single error. - ★ 515 (Miscellaneous)
- fucking-awesome-go - :octocat: go-multierror - A Go (golang) package for representing a list of errors as a single error. :star: 194 :fork_and_knife: 10 (Miscellaneous / Advanced Console UIs)
- awesome-go-cn - go-multierror
- awesome-go - hashicorp/go-multierror
- awesome-go - go-multierror - | - | - | (Error Handling / Advanced Console UIs)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-ccamel - hashicorp/go-multierror - A Go (golang) package for representing a list of errors as a single error. (Go)
- awesome-go-cn - go-multierror
- fucking-awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-go-cn - go-multierror - multierror) (错误处理 / 检索及分析资料库)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. - :arrow_down:2643 - :star:205 (Error Handling / Advanced Console UIs)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Advanced Console UIs)
- awesome-go-zh - go-multierror
- awesome-go-extra - go-multierror - 12-15T20:12:26Z|2022-07-08T08:51:58Z| (Error Handling / Advanced Console UIs)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (<span id="其他-miscellaneous">其他 Miscellaneous</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Miscellaneous / Advanced Console UIs)
- awesome-go-with-stars - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-go - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Search and Analytic Databases)
- awesome-Char - go-multierror - Go (golang) package for representing a list of errors as a single error. (Error Handling / Advanced Console UIs)
README
# go-multierror
[](https://circleci.com/gh/hashicorp/go-multierror)
[](https://pkg.go.dev/github.com/hashicorp/go-multierror)

[circleci]: https://app.circleci.com/pipelines/github/hashicorp/go-multierror
[godocs]: https://pkg.go.dev/github.com/hashicorp/go-multierror
`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 fully compatible with the Go standard library
[errors](https://golang.org/pkg/errors/) package, including the
functions `As`, `Is`, and `Unwrap`. This provides a standardized approach
for introspecting on error values.
## 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](https://golang.org/doc/go1.13#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](https://github.com/hashicorp/go-multierror/tree/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.
```go
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:
```go
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:
```go
if err := something(); err != nil {
if merr, ok := err.(*multierror.Error); ok {
// Use merr.Errors
}
}
```
You can also use the standard [`errors.Unwrap`](https://golang.org/pkg/errors/#Unwrap)
function. This will continue to unwrap into subsequent errors until none exist.
**Extracting an error**
The standard library [`errors.As`](https://golang.org/pkg/errors/#As)
function can be used directly with a multierror to extract a specific error:
```go
// 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`](https://golang.org/pkg/os/#pkg-variables)
error in the `os` package. You can check if this error is present by using
the standard [`errors.Is`](https://golang.org/pkg/errors/#Is) function.
```go
// 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:
```go
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()
```