-
Notifications
You must be signed in to change notification settings - Fork 5
/
errors_test.go
186 lines (167 loc) · 4.54 KB
/
errors_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
// Package errors helps in wrapping errors with custom type as well as a user friendly message. This is particularly useful when responding to APIs
package errors
import (
"errors"
"fmt"
"reflect"
"strings"
"testing"
)
func TestFormat(t *testing.T) {
bar := func() error {
return fmt.Errorf("hello %s", "world!")
}
foo := func() error {
err := bar()
if err != nil {
return InternalErr(err, "bar is not happy")
}
return nil
}
err := foo()
got := fmt.Sprintf("%+v", err)
want := "errors/errors_test.go:21: bar is not happy\nhello world!"
if !strings.Contains(got, want) {
t.Errorf("got %q\nwant %q", got, want)
}
got = fmt.Sprintf("%v", err)
want = "bar is not happy"
if !strings.Contains(got, want) {
t.Errorf("got %q\nwant %q", got, want)
}
got = fmt.Sprintf("%+s", err)
want = "bar is not happy: hello world!"
if !strings.Contains(got, want) {
t.Errorf("got %q\nwant %q", got, want)
}
got = fmt.Sprintf("%s", err)
want = "bar is not happy"
if !strings.Contains(got, want) {
t.Errorf("got %q\nwant %q", got, want)
}
}
func TestErrorWithoutFileLine(t *testing.T) {
err := New("error without file line")
want := "error without file line"
got := err.ErrorWithoutFileLine()
if got != want {
t.Errorf("ErrorWithoutFileLine() = %v\nwant %v", got, want)
}
err = Wrap(err, "wrapped error")
want = "wrapped error: error without file line"
got = err.ErrorWithoutFileLine()
if got != want {
t.Errorf("ErrorWithoutFileLine() = %v\nwant %v", got, want)
}
err = Wrap(errors.New("std err"), "wrapped std error")
want = "wrapped std error: std err"
got = err.ErrorWithoutFileLine()
if got != want {
t.Errorf("ErrorWithoutFileLine() = %v\nwant %v", got, want)
}
err = Wrap(errors.New("std err"), "")
want = "std err"
got = err.ErrorWithoutFileLine()
if got != want {
t.Errorf("ErrorWithoutFileLine() = %v\nwant %v", got, want)
}
err = New("")
got = err.ErrorWithoutFileLine()
if !strings.Contains(got, "errors/errors_test.go:") {
t.Errorf("empty error should have fileline: %s", got)
}
}
func TestNew(t *testing.T) {
message := "friendly error message"
want := Error{
message: message,
eType: defaultErrType,
}
e := New(message)
e.pcs = nil
e.pc = 0
if !reflect.DeepEqual(*e, want) {
t.Errorf("New() = %v\nwant %v", *e, want)
}
}
func TestErrorf(t *testing.T) {
format := "%s prefixed"
message := "friendly error message"
want := Error{
message: fmt.Sprintf(format, message),
eType: defaultErrType,
}
e := Errorf(format, message)
e.pcs = nil
e.pc = 0
if !reflect.DeepEqual(*e, want) {
t.Fail()
}
}
func TestSetDefaultType(t *testing.T) {
type args struct {
message string
e errType
}
tests := []struct {
name string
args args
wantErrType errType
}{
{
name: "TypeInputBody",
args: args{
e: TypeInputBody,
},
wantErrType: TypeInputBody,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
before := defaultErrType
SetDefaultType(tt.args.e)
err := New(tt.args.message)
// resetting to previous value to stop messing with the entire package
SetDefaultType(before)
if err.Type() != tt.wantErrType {
t.Errorf(
"New() = got type '%d', expected '%d",
err.Type(),
tt.wantErrType,
)
}
})
}
}
func TestStacktrace(t *testing.T) {
err := errors.New("original error")
e := Wrap(err, "wrapped error")
got := Stacktrace(e)
// silly way of verifying the stacktrace is correct, excluding filepaths
strings.Contains(got, "errors.TestStacktrace(): wrapped error")
strings.Contains(got, "errors/errors_test.go:76")
strings.Contains(got, "original error")
}
func TestStacktraceNoFormat(t *testing.T) {
err := errors.New("original error")
e := Wrap(err, "wrapped error")
got := strings.Join(StacktraceNoFormat(e), "#")
// silly way of verifying the stacktrace is correct, excluding filepaths
strings.Contains(got, "errors.TestStacktrace(): wrapped error")
strings.Contains(got, "errors/errors_test.go:76")
strings.Contains(got, "original error")
if strings.Contains(got, "\n") {
t.Error("StacktraceNoFormat() should not contain newlines")
}
}
func TestStacktraceCustomFormat(t *testing.T) {
err := errors.New("original error")
e := Wrap(err, "wrapped error")
msgFormat := "message: %m#"
traceFormat := "function: %f|"
got := StacktraceCustomFormat(msgFormat, traceFormat, e)
want := "message: wrapped error#function: github.com/naughtygopher/errors.TestStacktraceCustomFormat|function: testing.tRunner|message: original error#"
if got != want {
t.Errorf("StacktraceCustomFormat() = %v\nwant %v", got, want)
}
}