errors.go 884 B

1234567891011121314151617181920212223242526272829
  1. package errutil
  2. import (
  3. "fmt"
  4. "golang.org/x/xerrors"
  5. )
  6. // Wrap is a simple wrapper around Errorf that is doing error wrapping. You can read how that works in
  7. // https://godoc.org/golang.org/x/xerrors#Errorf but its API is very implicit which is a reason for this wrapper.
  8. // There is also a discussion (https://github.com/golang/go/issues/29934) where many comments make arguments for such
  9. // wrapper so hopefully it will be added in the standard lib later.
  10. func Wrap(message string, err error) error {
  11. if err == nil {
  12. return nil
  13. }
  14. return xerrors.Errorf("%v: %w", message, err)
  15. }
  16. // Wrapf is a simple wrapper around Errorf that is doing error wrapping
  17. // Wrapf allows you to send a format and args instead of just a message.
  18. func Wrapf(err error, message string, a ...interface{}) error {
  19. if err == nil {
  20. return nil
  21. }
  22. return Wrap(fmt.Sprintf(message, a...), err)
  23. }