exception.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package raven
  2. import (
  3. "reflect"
  4. "regexp"
  5. )
  6. var errorMsgPattern = regexp.MustCompile(`\A(\w+): (.+)\z`)
  7. func NewException(err error, stacktrace *Stacktrace) *Exception {
  8. msg := err.Error()
  9. ex := &Exception{
  10. Stacktrace: stacktrace,
  11. Value: msg,
  12. Type: reflect.TypeOf(err).String(),
  13. }
  14. if m := errorMsgPattern.FindStringSubmatch(msg); m != nil {
  15. ex.Module, ex.Value = m[1], m[2]
  16. }
  17. return ex
  18. }
  19. // https://docs.getsentry.com/hosted/clientdev/interfaces/#failure-interfaces
  20. type Exception struct {
  21. // Required
  22. Value string `json:"value"`
  23. // Optional
  24. Type string `json:"type,omitempty"`
  25. Module string `json:"module,omitempty"`
  26. Stacktrace *Stacktrace `json:"stacktrace,omitempty"`
  27. }
  28. func (e *Exception) Class() string { return "exception" }
  29. func (e *Exception) Culprit() string {
  30. if e.Stacktrace == nil {
  31. return ""
  32. }
  33. return e.Stacktrace.Culprit()
  34. }
  35. // Exceptions allows for chained errors
  36. // https://docs.sentry.io/clientdev/interfaces/exception/
  37. type Exceptions struct {
  38. // Required
  39. Values []*Exception `json:"values"`
  40. }
  41. func (es Exceptions) Class() string { return "exception" }