]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
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 | } |