-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
80 lines (65 loc) · 1.28 KB
/
errors.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
package logrus_rollbar
import (
"fmt"
"runtime"
"strconv"
"github.com/pkg/errors"
"github.com/rollbar/rollbar-go"
)
// Rollbar package expect such error:
// type CauseStacker interface {
// error
// Cause() error
// Stack() Stack
// }
var (
_ rollbar.CauseStacker = wrappedError{}
)
type wrappedError struct {
err error
msg string
}
func (err wrappedError) Error() string {
return err.msg
}
type causer interface {
Cause() error
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
func Wrap(msg string, err error) wrappedError {
return wrappedError{msg: msg, err: err}
}
func (err wrappedError) Cause() error {
return err.err
}
func (werr wrappedError) Stack() []runtime.Frame {
stack := []runtime.Frame{}
err := werr.err
// We're going to the deepest call
for {
c, ok := err.(causer)
if !ok {
break
}
err = c.Cause()
}
// Return an empty stack
tracer, ok := err.(stackTracer)
if !ok {
return stack
}
errorsStack := tracer.StackTrace()
for i := len(errorsStack) - 1; i >= 0; i-- {
f := errorsStack[i]
line, _ := strconv.Atoi(fmt.Sprintf("%d", f))
frame := runtime.Frame{
File: fmt.Sprintf("%+s", f),
Line: line,
Function: fmt.Sprintf("%n", f),
}
stack = append([]runtime.Frame{frame}, stack...)
}
return stack
}