-
Notifications
You must be signed in to change notification settings - Fork 0
/
uhoh_test.go
236 lines (182 loc) · 5.38 KB
/
uhoh_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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package uhoh
import (
"errors"
"fmt"
"os"
"testing"
"time"
)
func Example() {
// Errors
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
// Create error
err := New(originalErr)
err.SetDescribe(describeErr)
err.SetType(ErrGeneral)
// Can set date if need be
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 20, 30, 0, time.UTC))
// Output info
fmt.Println(err.Error())
fmt.Println(err.Original)
fmt.Println(err.Describe)
fmt.Println(err.Stack)
// Output:
// 2021-09-12T01:20:30Z | general error | original error | describe error
// original error
// describe error
// [{uhoh_test.go Example 17} {run_example.go runExample 64} {example.go runExamples 44}]
}
func Example_realWorld() {
_, err := os.Open("/test.txt")
if err != nil {
uhohErr := New(err).SetDescribe(errors.New("Failed to open file. Please check settings."))
uhohErr.SetDate(time.Date(2021, time.Month(9), 12, 1, 20, 30, 0, time.UTC))
fmt.Printf("%s", uhohErr.ToJSON())
}
// Output:
// {"date":"2021-09-12T01:20:30Z","describe":"Failed to open file. Please check settings.","original":"open /test.txt: no such file or directory","stack":[{"file":"uhoh_test.go","function":"Example_realWorld","line":40},{"file":"run_example.go","function":"runExample","line":64},{"file":"example.go","function":"runExamples","line":44}]}
}
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
original := errors.New("original")
describe := errors.New("describe")
New(original).SetDescribe(describe).SetType(ErrGeneral)
}
}
func ExampleNew() {
// Original error
originalErr := errors.New("original error")
err := New(originalErr)
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 20, 30, 0, time.UTC))
fmt.Println(err.Error())
// Output:
// 2021-09-12T01:20:30Z | original error
}
func ExampleNewStr() {
err := NewStr("original error")
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 20, 30, 0, time.UTC))
fmt.Println(err.Error())
// Output:
// 2021-09-12T01:20:30Z | original error
}
func ExampleNewStackLevel() {
// Original error
originalErr := errors.New("original error")
// Create error with stack level
err := NewStackLevel(originalErr, 1)
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 20, 30, 0, time.UTC))
fmt.Println(err.Error())
// Output:
// 2021-09-12T01:20:30Z | original error
}
func ExampleErr_Original() {
// Original error
originalErr := errors.New("original error")
err := New(originalErr)
fmt.Println(err.Original)
// Output:
// original error
}
func ExampleErr_Describe() {
// Errors
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
err := New(originalErr).SetDescribe(describeErr)
fmt.Println(err.Describe)
// Output:
// describe error
}
func ExampleErr_SetDescribe() {
// Errors
originalErr := errors.New("original error")
err := New(originalErr)
err.SetDescribe(errors.New("new describe error"))
fmt.Println(err.Describe)
// Output:
// new describe error
}
func ExampleErr_SetDescribeStr() {
// Errors
originalErr := errors.New("original error")
err := New(originalErr)
err.SetDescribeStr("new describe error")
fmt.Println(err.Describe)
// Output:
// new describe error
}
func ExampleErr_Type() {
// Original error
originalErr := errors.New("original error")
err := New(originalErr).SetType(ErrGeneral)
fmt.Println(err.Type)
// Output:
// general error
}
func ExampleErr_SetType() {
// Errors
originalErr := errors.New("original error")
err := New(originalErr).SetType(ErrGeneral)
fmt.Println(err.Type)
// Output:
// general error
}
func ExampleErr_SetTypeStr() {
// Errors
originalErr := errors.New("original error")
err := New(originalErr).SetTypeStr("new type")
fmt.Println(err.Type)
// Output:
// new type
}
func ExampleErr_Date() {
// Errors
originalErr := errors.New("original error")
err := New(originalErr)
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 20, 30, 0, time.UTC))
fmt.Println(err.Date)
// Output:
// 2021-09-12 01:20:30 +0000 UTC
}
func ExampleErr_Unwrap() {
// Errors
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
err := New(originalErr).SetDescribe(describeErr)
fmt.Println(err.Unwrap().Error())
// Output:
// original error
}
func TestNewPassInExistingErr(t *testing.T) {
// Create first uhoh error
original := errors.New("original")
describe := errors.New("describe")
firstErr := New(original).SetDescribe(describe).SetType(ErrGeneral)
// Create second uhoh error and pass in first uhoh error
secondErr := New(firstErr)
// Check if second uhoh error is the same as first uhoh error
if firstErr.Date.Format(time.RFC3339) != secondErr.Date.Format(time.RFC3339) {
t.Errorf("Date should be the same")
}
}
func TestIs(t *testing.T) {
original := errors.New("original")
describe := errors.New("describe")
err := New(original).SetDescribe(describe).SetType(ErrGeneral)
// Check to make sure Is is either original or describe
if !errors.Is(err, original) && !errors.Is(err, describe) && !errors.Is(err, ErrGeneral) {
t.Error("Error should be original, describe or type error")
}
// Check if is original error
if !err.IsOriginal(original) {
t.Error("Error did not match original")
}
// Check if is describe error
if !err.IsDescribe(describe) {
t.Error("Error did not match describe")
}
// Check if is type error
if !err.IsType(ErrGeneral) {
t.Error("Error did not match type")
}
}