]>
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: | |
107c1cdb ND |
21 | if e != nil { |
22 | err.Errors = append(err.Errors, e.Errors...) | |
23 | } | |
bae9f6d2 | 24 | default: |
107c1cdb ND |
25 | if e != nil { |
26 | err.Errors = append(err.Errors, e) | |
27 | } | |
bae9f6d2 JC |
28 | } |
29 | } | |
30 | ||
31 | return err | |
32 | default: | |
33 | newErrs := make([]error, 0, len(errs)+1) | |
34 | if err != nil { | |
35 | newErrs = append(newErrs, err) | |
36 | } | |
37 | newErrs = append(newErrs, errs...) | |
38 | ||
39 | return Append(&Error{}, newErrs...) | |
40 | } | |
41 | } |