diff options
Diffstat (limited to 'vendor/github.com/hashicorp/go-multierror/prefix.go')
-rw-r--r-- | vendor/github.com/hashicorp/go-multierror/prefix.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/go-multierror/prefix.go b/vendor/github.com/hashicorp/go-multierror/prefix.go new file mode 100644 index 0000000..5c477ab --- /dev/null +++ b/vendor/github.com/hashicorp/go-multierror/prefix.go | |||
@@ -0,0 +1,37 @@ | |||
1 | package multierror | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "github.com/hashicorp/errwrap" | ||
7 | ) | ||
8 | |||
9 | // Prefix is a helper function that will prefix some text | ||
10 | // to the given error. If the error is a multierror.Error, then | ||
11 | // it will be prefixed to each wrapped error. | ||
12 | // | ||
13 | // This is useful to use when appending multiple multierrors | ||
14 | // together in order to give better scoping. | ||
15 | func Prefix(err error, prefix string) error { | ||
16 | if err == nil { | ||
17 | return nil | ||
18 | } | ||
19 | |||
20 | format := fmt.Sprintf("%s {{err}}", prefix) | ||
21 | switch err := err.(type) { | ||
22 | case *Error: | ||
23 | // Typed nils can reach here, so initialize if we are nil | ||
24 | if err == nil { | ||
25 | err = new(Error) | ||
26 | } | ||
27 | |||
28 | // Wrap each of the errors | ||
29 | for i, e := range err.Errors { | ||
30 | err.Errors[i] = errwrap.Wrapf(format, e) | ||
31 | } | ||
32 | |||
33 | return err | ||
34 | default: | ||
35 | return errwrap.Wrapf(format, err) | ||
36 | } | ||
37 | } | ||