-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.go
205 lines (181 loc) · 6.78 KB
/
stack_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package eris_test
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/risingwavelabs/eris"
)
const (
file = "eris/stack_test.go"
readFunc = "eris_test.ReadFile"
parseFunc = "eris_test.ParseFile"
processFunc = "eris_test.ProcessFile"
globalTestFunc = "eris_test.TestGlobalStack"
localTestFunc = "eris_test.TestLocalStack"
extGlobalTestFunc = "eris_test.TestExtGlobalStack"
extLocalTestFunc = "eris_test.TestExtLocalStack"
)
var (
errEOF = eris.New("unexpected EOF").WithCode(eris.CodeUnknown)
errExt = errors.New("external error")
)
// example func that either returns a wrapped global or creates/wraps a new local error.
func ReadFile(fname string, global bool, external bool) error {
var err error
if !external && !global { // local eris
err = eris.New("unexpected EOF").WithCode(eris.CodeUnknown)
} else if !external && global { // global eris
err = errEOF
} else if external && !global { // local external
err = fmt.Errorf("external context: %w", errors.New("external error"))
} else { // global external
err = fmt.Errorf("external context: %w", errExt)
}
return eris.WithCode(eris.Wrapf(err, "error reading file '%v'", fname), eris.CodeUnknown)
}
// example func that just catches and returns an error.
func ParseFile(fname string, global bool, external bool) error {
err := ReadFile(fname, global, external)
if err != nil {
return err
}
return nil
}
// example func that wraps an error with additional context.
func ProcessFile(fname string, global bool, external bool) error {
// parse the file
err := ParseFile(fname, global, external)
if err != nil {
return eris.WithCode(eris.Wrapf(err, "error processing file '%v'", fname), eris.CodeUnknown)
}
return nil
}
func TestGlobalStack(t *testing.T) {
// expected results
expectedChain := []eris.StackFrame{
{Name: readFunc, File: file, Line: 41},
{Name: processFunc, File: file, Line: 58},
}
expectedRoot := []eris.StackFrame{
{Name: readFunc, File: file, Line: 41},
{Name: parseFunc, File: file, Line: 46},
{Name: processFunc, File: file, Line: 56},
{Name: processFunc, File: file, Line: 58},
{Name: globalTestFunc, File: file, Line: 77},
}
err := ProcessFile("example.json", true, false)
uerr := eris.Unpack(err)
validateWrapFrames(t, expectedChain, uerr)
validateRootStack(t, expectedRoot, uerr)
}
func TestLocalStack(t *testing.T) {
// expected results
expectedChain := []eris.StackFrame{
{Name: readFunc, File: file, Line: 41},
{Name: processFunc, File: file, Line: 58},
}
expectedRoot := []eris.StackFrame{
{Name: readFunc, File: file, Line: 33},
{Name: readFunc, File: file, Line: 41},
{Name: parseFunc, File: file, Line: 46},
{Name: processFunc, File: file, Line: 56},
{Name: processFunc, File: file, Line: 58},
{Name: localTestFunc, File: file, Line: 98},
}
err := ProcessFile("example.json", false, false)
uerr := eris.Unpack(err)
validateWrapFrames(t, expectedChain, uerr)
validateRootStack(t, expectedRoot, uerr)
}
func TestExtGlobalStack(t *testing.T) {
// expected results
expectedChain := []eris.StackFrame{
{Name: processFunc, File: file, Line: 58},
}
expectedRoot := []eris.StackFrame{
{Name: readFunc, File: file, Line: 41},
{Name: parseFunc, File: file, Line: 46},
{Name: processFunc, File: file, Line: 56},
{Name: processFunc, File: file, Line: 58},
{Name: extGlobalTestFunc, File: file, Line: 117},
}
err := ProcessFile("example.json", true, true)
uerr := eris.Unpack(err)
validateWrapFrames(t, expectedChain, uerr)
validateRootStack(t, expectedRoot, uerr)
}
func TestExtLocalStack(t *testing.T) {
// expected results
expectedChain := []eris.StackFrame{
{Name: processFunc, File: file, Line: 58},
}
expectedRoot := []eris.StackFrame{
{Name: readFunc, File: file, Line: 41},
{Name: parseFunc, File: file, Line: 46},
{Name: processFunc, File: file, Line: 56},
{Name: processFunc, File: file, Line: 58},
{Name: extLocalTestFunc, File: file, Line: 136},
}
err := ProcessFile("example.json", false, true)
uerr := eris.Unpack(err)
validateWrapFrames(t, expectedChain, uerr)
validateRootStack(t, expectedRoot, uerr)
}
func validateWrapFrames(t *testing.T, expectedChain []eris.StackFrame, uerr eris.UnpackedError) {
// verify the expected and actual error chain have the same length
if len(expectedChain) != len(uerr.ErrChain) {
t.Fatalf("%v: expected number of wrapped frames { %v } got { %v }", localTestFunc, len(expectedChain), len(uerr.ErrChain))
}
// verify the wrapped frames match expected values
for i := 0; i < len(expectedChain); i++ {
if expectedChain[i].Name != uerr.ErrChain[i].Frame.Name {
t.Errorf("%v: expected wrap func name { %v } got { %v }", localTestFunc, expectedChain[i].Name, uerr.ErrChain[i].Frame.Name)
}
if !strings.Contains(uerr.ErrChain[i].Frame.File, expectedChain[i].File) {
t.Errorf("%v: expected wrap file name to contain { %v } got { %v }", localTestFunc, uerr.ErrChain[i].Frame.File, expectedChain[i].File)
}
if expectedChain[i].Line != uerr.ErrChain[i].Frame.Line {
t.Errorf("%v: expected wrap line number { %v } got { %v }", localTestFunc, expectedChain[i].Line, uerr.ErrChain[i].Frame.Line)
}
}
}
func validateRootStack(t *testing.T, expectedRoot []eris.StackFrame, uerr eris.UnpackedError) {
// verify the expected and actual stack have the same length
if len(expectedRoot) != len(uerr.ErrRoot.Stack) {
t.Fatalf("%v: expected number of root error frames { %v } got { %v }", localTestFunc, len(expectedRoot), len(uerr.ErrRoot.Stack))
}
// verify the root error stack frames match expected values
for i := 0; i < len(expectedRoot); i++ {
if expectedRoot[i].Name != uerr.ErrRoot.Stack[i].Name {
t.Errorf("%v: expected root func name { %v } got { %v }", localTestFunc, expectedRoot[i].Name, uerr.ErrRoot.Stack[i].Name)
}
if !strings.Contains(uerr.ErrRoot.Stack[i].File, expectedRoot[i].File) {
t.Errorf("%v: expected root file name to contain { %v } got { %v }", localTestFunc, uerr.ErrRoot.Stack[i].File, expectedRoot[i].File)
}
if expectedRoot[i].Line != uerr.ErrRoot.Stack[i].Line {
t.Errorf("%v: expected root line number { %v } got { %v }", localTestFunc, expectedRoot[i].Line, uerr.ErrRoot.Stack[i].Line)
}
}
}
func TestGoRoutines(t *testing.T) {
expectedChain := []eris.StackFrame{
{Name: "eris_test.TestGoRoutines.func1", File: file, Line: 192},
}
expectedRoot := []eris.StackFrame{
{Name: "eris_test.dummyStack", File: file, Line: 204},
}
go func() {
err := dummyStack()
err = eris.WithCode(eris.Wrap(err, "error reading file"), eris.CodeUnknown)
// verify the stack frames match expected values
uerr := eris.Unpack(err)
validateWrapFrames(t, expectedChain, uerr)
validateRootStack(t, expectedRoot, uerr)
}()
time.Sleep(250 * time.Millisecond)
}
func dummyStack() error {
return eris.New("unexpected EOF").WithCode(eris.CodeUnknown)
}