errors.go 756 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Package errors implements functions to manipulate errors.
  2. package errors
  3. type errorString string
  4. func (e errorString) Error() string {
  5. return string(e)
  6. }
  7. // New returns an error that formats as the given text.
  8. func New(text string) error {
  9. return errorString(text)
  10. }
  11. // Cause returns the underlying cause of the error, if possible.
  12. // An error value has a cause if it implements the following
  13. // method:
  14. //
  15. // Cause() error
  16. //
  17. //
  18. // If the error does not implement Cause, the original error will
  19. // be returned. If the error is nil, nil will be returned without further
  20. // investigation.
  21. func Cause(err error) error {
  22. if err == nil {
  23. return nil
  24. }
  25. if err, ok := err.(interface {
  26. Cause() error
  27. }); ok {
  28. return err.Cause()
  29. }
  30. return err
  31. }