]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/hashicorp/go-multierror/append.go
Merge pull request #27 from terraform-providers/go-modules-2019-02-22
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / go-multierror / append.go
CommitLineData
bae9f6d2
JC
1package 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.
9func 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}