-
Notifications
You must be signed in to change notification settings - Fork 13
/
unwrap_test.go
100 lines (81 loc) · 2.35 KB
/
unwrap_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
94
95
96
97
98
99
100
package errtrace
import (
"errors"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestUnwrapFrame(t *testing.T) {
giveErr := errors.New("great sadness")
t.Run("not wrapped", func(t *testing.T) {
_, inner, ok := UnwrapFrame(giveErr)
if got, want := ok, false; got != want {
t.Errorf("ok: got %v, want %v", got, want)
}
if got, want := inner, giveErr; got != want {
t.Errorf("inner: got %v, want %v", inner, giveErr)
}
})
t.Run("wrapped", func(t *testing.T) {
wrapped := Wrap(giveErr)
frame, inner, ok := UnwrapFrame(wrapped)
if got, want := ok, true; got != want {
t.Errorf("ok: got %v, want %v", got, want)
}
if got, want := inner, giveErr; got != want {
t.Errorf("inner: got %v, want %v", inner, giveErr)
}
if got, want := frame.Function, ".TestUnwrapFrame.func2"; !strings.HasSuffix(got, want) {
t.Errorf("frame.Func: got %q, does not contain %q", got, want)
}
if got, want := filepath.Base(frame.File), "unwrap_test.go"; got != want {
t.Errorf("frame.File: got %v, want %v", got, want)
}
})
t.Run("custom error", func(t *testing.T) {
wrapped := wrapCustomTrace(giveErr)
frame, inner, ok := UnwrapFrame(wrapped)
if got, want := ok, true; got != want {
t.Errorf("ok: got %v, want %v", got, want)
}
if got, want := inner, giveErr; got != want {
t.Errorf("inner: got %v, want %v", inner, giveErr)
}
if got, want := frame.Function, ".wrapCustomTrace"; !strings.HasSuffix(got, want) {
t.Errorf("frame.Func: got %q, does not contain %q", got, want)
}
if got, want := filepath.Base(frame.File), "unwrap_test.go"; got != want {
t.Errorf("frame.File: got %v, want %v", got, want)
}
})
}
func TestUnwrapFrame_badPC(t *testing.T) {
giveErr := errors.New("great sadness")
_, inner, ok := UnwrapFrame(wrap(giveErr, 0))
if got, want := ok, false; got != want {
t.Errorf("ok: got %v, want %v", got, want)
}
if got, want := inner, giveErr; got != want {
t.Errorf("inner: got %v, want %v", inner, giveErr)
}
}
type customTraceError struct {
err error
pc uintptr
}
func wrapCustomTrace(err error) error {
return &customTraceError{
err: err,
pc: reflect.ValueOf(wrapCustomTrace).Pointer(),
}
}
func (e *customTraceError) Error() string {
return e.err.Error()
}
func (e *customTraceError) TracePC() uintptr {
return e.pc
}
func (e *customTraceError) Unwrap() error {
return e.err
}