-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
73 lines (62 loc) · 1.26 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
package honeybadger
import (
"fmt"
"reflect"
"runtime"
"strconv"
)
const maxFrames = 20
// Frame represent a stack frame inside of a Honeybadger backtrace.
type Frame struct {
Number string `json:"number"`
File string `json:"file"`
Method string `json:"method"`
}
// Error provides more structured information about a Go error.
type Error struct {
err interface{}
Message string
Class string
Stack []*Frame
}
func (e Error) Error() string {
return e.Message
}
func NewError(msg interface{}) Error {
return newError(msg, 2)
}
func newError(thing interface{}, stackOffset int) Error {
var err error
switch t := thing.(type) {
case Error:
return t
case error:
err = t
default:
err = fmt.Errorf("%v", t)
}
return Error{
err: err,
Message: err.Error(),
Class: reflect.TypeOf(err).String(),
Stack: generateStack(stackOffset),
}
}
func generateStack(offset int) (frames []*Frame) {
stack := make([]uintptr, maxFrames)
length := runtime.Callers(2+offset, stack[:])
for _, pc := range stack[:length] {
f := runtime.FuncForPC(pc)
if f == nil {
continue
}
file, line := f.FileLine(pc)
frame := &Frame{
File: file,
Number: strconv.Itoa(line),
Method: f.Name(),
}
frames = append(frames, frame)
}
return
}