-
Notifications
You must be signed in to change notification settings - Fork 16
/
error_test.go
93 lines (77 loc) · 2.11 KB
/
error_test.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
87
88
89
90
91
92
93
package honeybadger
import (
"fmt"
"runtime"
"strings"
"testing"
)
func TestNewErrorTrace(t *testing.T) {
fn := func() Error {
return NewError("Error msg")
}
err := fn()
// The stack should look like this:
// github.com/honeybadger-io/honeybadger-go.TestNewErrorTrace.func1
// github.com/honeybadger-io/honeybadger-go.TestNewErrorTrace
// testing.tRunner
// runtime.goexit
if len(err.Stack) < 3 {
t.Errorf("Expected to generate full trace")
}
// Checks that the top top methods are the (inlined) fn and the test Method
expected := []string{
".TestNewErrorTrace.func1",
".TestNewErrorTrace",
}
for i, suffix := range expected {
method := err.Stack[i].Method
if !strings.HasSuffix(method, suffix) {
// Logs the stack to give some context about the error
for j, stack := range err.Stack {
t.Logf("%d: %s", j, stack.Method)
}
t.Fatalf("stack[%d].Method expected_suffix=%q actual=%q", i, suffix, method)
}
}
}
type customerror struct {
error
callers []uintptr
}
func (t customerror) Callers() []uintptr {
return t.callers
}
func newcustomerror() customerror {
stack := make([]uintptr, maxFrames)
length := runtime.Callers(1, stack[:])
return customerror{
error: fmt.Errorf("hello world"),
callers: stack[:length],
}
}
func TestNewErrorCustomTrace(t *testing.T) {
err := NewError(newcustomerror())
// The stack should look like this:
// github.com/honeybadger-io/honeybadger-go.newcustomerror
// github.com/honeybadger-io/honeybadger-go.TestNewErrorCustomTrace
// testing.tRunner
// runtime.goexit
if len(err.Stack) < 3 {
t.Errorf("Expected to generate full trace")
}
// Checks that the top top methods are the (inlined) fn and the test Method
expected := []string{
".newcustomerror",
".TestNewErrorCustomTrace",
}
for i, suffix := range expected {
method := err.Stack[i].Method
if !strings.HasSuffix(method, suffix) {
// Logs the stack to give some context about the error
for j, stack := range err.Stack {
t.Logf("%d: %s", j, stack.Method)
}
t.Fatalf("stack[%d].Method expected_suffix=%q actual=%q", i, suffix, method)
}
}
}