• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D24-Aug-201899

LICENSEH A D24-Aug-201815.6 KiB

MakefileH A D24-Aug-2018821

README.mdH A D24-Aug-20182.7 KiB

append.goH A D24-Aug-2018962

append_test.goH A D24-Aug-20181.8 KiB

flatten.goH A D24-Aug-2018568

flatten_test.goH A D24-Aug-2018684

format.goH A D24-Aug-2018668

format_test.goH A D24-Aug-2018543

go.modH A D24-Aug-201887

go.sumH A D24-Aug-2018406

multierror.goH A D24-Aug-20181.2 KiB

multierror_test.goH A D24-Aug-20181.2 KiB

prefix.goH A D24-Aug-2018794

prefix_test.goH A D24-Aug-2018597

sort.goH A D24-Aug-2018452

sort_test.goH A D24-Aug-2018681

README.md

1# go-multierror
2
3[![Build Status](http://img.shields.io/travis/hashicorp/go-multierror.svg?style=flat-square)][travis]
4[![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs]
5
6[travis]: https://travis-ci.org/hashicorp/go-multierror
7[godocs]: https://godoc.org/github.com/hashicorp/go-multierror
8
9`go-multierror` is a package for Go that provides a mechanism for
10representing a list of `error` values as a single `error`.
11
12This allows a function in Go to return an `error` that might actually
13be a list of errors. If the caller knows this, they can unwrap the
14list and access the errors. If the caller doesn't know, the error
15formats to a nice human-readable format.
16
17`go-multierror` implements the
18[errwrap](https://github.com/hashicorp/errwrap) interface so that it can
19be used with that library, as well.
20
21## Installation and Docs
22
23Install using `go get github.com/hashicorp/go-multierror`.
24
25Full documentation is available at
26http://godoc.org/github.com/hashicorp/go-multierror
27
28## Usage
29
30go-multierror is easy to use and purposely built to be unobtrusive in
31existing Go applications/libraries that may not be aware of it.
32
33**Building a list of errors**
34
35The `Append` function is used to create a list of errors. This function
36behaves a lot like the Go built-in `append` function: it doesn't matter
37if the first argument is nil, a `multierror.Error`, or any other `error`,
38the function behaves as you would expect.
39
40```go
41var result error
42
43if err := step1(); err != nil {
44	result = multierror.Append(result, err)
45}
46if err := step2(); err != nil {
47	result = multierror.Append(result, err)
48}
49
50return result
51```
52
53**Customizing the formatting of the errors**
54
55By specifying a custom `ErrorFormat`, you can customize the format
56of the `Error() string` function:
57
58```go
59var result *multierror.Error
60
61// ... accumulate errors here, maybe using Append
62
63if result != nil {
64	result.ErrorFormat = func([]error) string {
65		return "errors!"
66	}
67}
68```
69
70**Accessing the list of errors**
71
72`multierror.Error` implements `error` so if the caller doesn't know about
73multierror, it will work just fine. But if you're aware a multierror might
74be returned, you can use type switches to access the list of errors:
75
76```go
77if err := something(); err != nil {
78	if merr, ok := err.(*multierror.Error); ok {
79		// Use merr.Errors
80	}
81}
82```
83
84**Returning a multierror only if there are errors**
85
86If you build a `multierror.Error`, you can use the `ErrorOrNil` function
87to return an `error` implementation only if there are errors to return:
88
89```go
90var result *multierror.Error
91
92// ... accumulate errors here
93
94// Return the `error` only if errors were added to the multierror, otherwise
95// return nil since there are no errors.
96return result.ErrorOrNil()
97```
98