errors.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package raven
  2. type causer interface {
  3. Cause() error
  4. }
  5. type errWrappedWithExtra struct {
  6. err error
  7. extraInfo map[string]interface{}
  8. }
  9. func (ewx *errWrappedWithExtra) Error() string {
  10. return ewx.err.Error()
  11. }
  12. func (ewx *errWrappedWithExtra) Cause() error {
  13. return ewx.err
  14. }
  15. func (ewx *errWrappedWithExtra) ExtraInfo() Extra {
  16. return ewx.extraInfo
  17. }
  18. // Adds extra data to an error before reporting to Sentry
  19. func WrapWithExtra(err error, extraInfo map[string]interface{}) error {
  20. return &errWrappedWithExtra{
  21. err: err,
  22. extraInfo: extraInfo,
  23. }
  24. }
  25. type ErrWithExtra interface {
  26. Error() string
  27. Cause() error
  28. ExtraInfo() Extra
  29. }
  30. // Iteratively fetches all the Extra data added to an error,
  31. // and it's underlying errors. Extra data defined first is
  32. // respected, and is not overridden when extracting.
  33. func extractExtra(err error) Extra {
  34. extra := Extra{}
  35. currentErr := err
  36. for currentErr != nil {
  37. if errWithExtra, ok := currentErr.(ErrWithExtra); ok {
  38. for k, v := range errWithExtra.ExtraInfo() {
  39. extra[k] = v
  40. }
  41. }
  42. if errWithCause, ok := currentErr.(causer); ok {
  43. currentErr = errWithCause.Cause()
  44. } else {
  45. currentErr = nil
  46. }
  47. }
  48. return extra
  49. }