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