-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
53 lines (46 loc) · 1.2 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
package lumigotracer
import (
"runtime"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// ErrInvalidToken an error about a missing token
var ErrInvalidToken = errors.New("invalid Token. Go to Lumigo Settings to get a valid token")
// defaultStackLength specifies the default maximum size of a stack trace.
const defaultStackLength = 64
func recoverWithLogs() {
if err := recover(); err != nil {
logger.WithFields(logrus.Fields{
"stacktrace": takeStacktrace(),
"error": err,
}).Error("an exception occurred in lumigo's code")
}
}
func takeStacktrace() string {
var builder strings.Builder
pcs := make([]uintptr, defaultStackLength)
// +2 to exclude runtime.Callers and takeStacktrace
numFrames := runtime.Callers(2+int(0), pcs)
if numFrames == 0 {
return ""
}
frames := runtime.CallersFrames(pcs[:numFrames])
for i := 0; ; i++ {
frame, more := frames.Next()
if i != 0 {
builder.WriteByte('\n')
}
builder.WriteString(frame.Function)
builder.WriteByte('\n')
builder.WriteByte('\t')
builder.WriteString(frame.File)
builder.WriteByte(':')
builder.WriteString(strconv.Itoa(frame.Line))
if !more {
break
}
}
return builder.String()
}