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