-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
86 lines (68 loc) · 1.52 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package rollbar
import (
"fmt"
"runtime"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/rollbar/rollbar-go"
)
type stackTracer interface {
Error() string
StackTrace() errors.StackTrace
}
type causeStacker struct {
err stackTracer
}
func newCauseStacker(err stackTracer) *causeStacker {
return &causeStacker{
err: err,
}
}
func (e *causeStacker) Error() string {
return e.err.Error()
}
func (e *causeStacker) Cause() error {
if c, ok := e.err.(interface{ Cause() error }); ok {
return c.Cause()
}
return nil
}
func (e *causeStacker) Stack() rollbar.Stack {
stackTrace := e.err.StackTrace()
stack := make(rollbar.Stack, len(stackTrace))
for i, frame := range stackTrace {
line, _ := strconv.Atoi(fmt.Sprintf("%d", frame))
stack[i] = rollbar.Frame{
Filename: shortenFilePath(strings.SplitN(fmt.Sprintf("%+s", frame), "\n\t", 2)[1]), // nolint: govet
Method: fmt.Sprintf("%n", frame), // nolint: govet
Line: line,
}
}
return stack
}
// copied from rollbar
// nolint: gochecknoglobals
var (
knownFilePathPatterns = []string{
"github.com/",
"code.google.com/",
"bitbucket.org/",
"launchpad.net/",
}
)
func shortenFilePath(s string) string {
// added to the original function
s = strings.TrimPrefix(s, runtime.GOROOT())
idx := strings.Index(s, "/src/pkg/")
if idx != -1 {
return s[idx+5:]
}
for _, pattern := range knownFilePathPatterns {
idx = strings.Index(s, pattern)
if idx != -1 {
return s[idx:]
}
}
return s
}