]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package multierror |
2 | ||
3 | // Append is a helper function that will append more errors | |
4 | // onto an Error in order to create a larger multi-error. | |
5 | // | |
6 | // If err is not a multierror.Error, then it will be turned into | |
7 | // one. If any of the errs are multierr.Error, they will be flattened | |
8 | // one level into err. | |
9 | func Append(err error, errs ...error) *Error { | |
10 | switch err := err.(type) { | |
11 | case *Error: | |
12 | // Typed nils can reach here, so initialize if we are nil | |
13 | if err == nil { | |
14 | err = new(Error) | |
15 | } | |
16 | ||
17 | // Go through each error and flatten | |
18 | for _, e := range errs { | |
19 | switch e := e.(type) { | |
20 | case *Error: | |
21 | err.Errors = append(err.Errors, e.Errors...) | |
22 | default: | |
23 | err.Errors = append(err.Errors, e) | |
24 | } | |
25 | } | |
26 | ||
27 | return err | |
28 | default: | |
29 | newErrs := make([]error, 0, len(errs)+1) | |
30 | if err != nil { | |
31 | newErrs = append(newErrs, err) | |
32 | } | |
33 | newErrs = append(newErrs, errs...) | |
34 | ||
35 | return Append(&Error{}, newErrs...) | |
36 | } | |
37 | } |