You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.0 KiB

  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. // Any nil errors within errs will be ignored. If err is nil, a new
  9. // *Error will be returned.
  10. func Append(err error, errs ...error) *Error {
  11. switch err := err.(type) {
  12. case *Error:
  13. // Typed nils can reach here, so initialize if we are nil
  14. if err == nil {
  15. err = new(Error)
  16. }
  17. // Go through each error and flatten
  18. for _, e := range errs {
  19. switch e := e.(type) {
  20. case *Error:
  21. if e != nil {
  22. err.Errors = append(err.Errors, e.Errors...)
  23. }
  24. default:
  25. if e != nil {
  26. err.Errors = append(err.Errors, e)
  27. }
  28. }
  29. }
  30. return err
  31. default:
  32. newErrs := make([]error, 0, len(errs)+1)
  33. if err != nil {
  34. newErrs = append(newErrs, err)
  35. }
  36. newErrs = append(newErrs, errs...)
  37. return Append(&Error{}, newErrs...)
  38. }
  39. }