cause.go 630 B

1234567891011121314151617181920212223242526272829
  1. // +build !go1.13
  2. package errors
  3. // Cause recursively unwraps an error chain and returns the underlying cause of
  4. // the error, if possible. An error value has a cause if it implements the
  5. // following interface:
  6. //
  7. // type causer interface {
  8. // Cause() error
  9. // }
  10. //
  11. // If the error does not implement Cause, the original error will
  12. // be returned. If the error is nil, nil will be returned without further
  13. // investigation.
  14. func Cause(err error) error {
  15. type causer interface {
  16. Cause() error
  17. }
  18. for err != nil {
  19. cause, ok := err.(causer)
  20. if !ok {
  21. break
  22. }
  23. err = cause.Cause()
  24. }
  25. return err
  26. }