append.go 740 B

123456789101112131415161718192021222324252627282930
  1. package multierror
  2. // Append is a helper function that will append more errors
  3. // onto an Error in order to create a larger multi-error.
  4. //
  5. // If err is not a multierror.Error, then it will be turned into
  6. // one. If any of the errs are multierr.Error, they will be flattened
  7. // one level into err.
  8. func Append(err error, errs ...error) *Error {
  9. switch err := err.(type) {
  10. case *Error:
  11. // Typed nils can reach here, so initialize if we are nil
  12. if err == nil {
  13. err = new(Error)
  14. }
  15. err.Errors = append(err.Errors, errs...)
  16. return err
  17. default:
  18. newErrs := make([]error, 0, len(errs)+1)
  19. if err != nil {
  20. newErrs = append(newErrs, err)
  21. }
  22. newErrs = append(newErrs, errs...)
  23. return &Error{
  24. Errors: newErrs,
  25. }
  26. }
  27. }