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.
 
 

38 lines
794 B

  1. package multierror
  2. import (
  3. "fmt"
  4. "github.com/hashicorp/errwrap"
  5. )
  6. // Prefix is a helper function that will prefix some text
  7. // to the given error. If the error is a multierror.Error, then
  8. // it will be prefixed to each wrapped error.
  9. //
  10. // This is useful to use when appending multiple multierrors
  11. // together in order to give better scoping.
  12. func Prefix(err error, prefix string) error {
  13. if err == nil {
  14. return nil
  15. }
  16. format := fmt.Sprintf("%s {{err}}", prefix)
  17. switch err := err.(type) {
  18. case *Error:
  19. // Typed nils can reach here, so initialize if we are nil
  20. if err == nil {
  21. err = new(Error)
  22. }
  23. // Wrap each of the errors
  24. for i, e := range err.Errors {
  25. err.Errors[i] = errwrap.Wrapf(format, e)
  26. }
  27. return err
  28. default:
  29. return errwrap.Wrapf(format, err)
  30. }
  31. }