@@ -8,133 +8,184 @@ import (
8
8
"testing"
9
9
)
10
10
11
+ // CustomError is a custom error type composed with Err.
12
+ type CustomError struct {
13
+ * Err
14
+ }
15
+
16
+ // NewCustomError returns a new CustomError and adds a stack trace.
17
+ func NewCustomError (message string ) error {
18
+ customError := CustomError {Err : & Err {Message : message }}
19
+ WithStack (customError .Err )
20
+ return customError
21
+ }
22
+
23
+ // CustomError2 is a custom error type composed with Err.
24
+ type CustomError2 struct {
25
+ * Err
26
+ }
27
+
28
+ // NewCustom2Error returns a new CustomError2 and adds a cause to the error.
29
+ func NewCustom2Error (message string , cause error ) error {
30
+ customError2 := CustomError2 {Err : & Err {Message : message }}
31
+ WithCause (customError2 .Err , cause )
32
+ return customError2
33
+ }
34
+
35
+ // customError3 is a custom error type not composed with Err.
36
+ type customError3 struct {}
37
+
38
+ func (e customError3 ) Error () string {
39
+ return "this is a custom error type"
40
+ }
41
+
11
42
func TestNew (t * testing.T ) {
12
- msg := "error message"
13
- if got := New (msg ).Error (); got != msg {
14
- t .Errorf (`wrong error message, got "%v", expected "%v"` , got , msg )
15
- return
16
- }
43
+ t .Run ("when New is provided with a message, it should create a new error with the message" , func (t * testing.T ) {
44
+ msg := "error message"
45
+ if got := New (msg ).Error (); got != msg {
46
+ t .Errorf (`wrong error message, got "%v", expected "%v"` , got , msg )
47
+ return
48
+ }
49
+ })
17
50
}
18
51
19
- func TestErrorc (t * testing.T ) {
20
- msg := "error message"
21
- data := Data {
22
- "id" : 1 ,
23
- "description" : "fool" ,
24
- }
25
-
26
- err := Errord (data , msg )
27
- if got := err .Error (); got != msg {
28
- t .Errorf (`wrong error message, got "%v", expected "%v"` , got , msg )
29
- return
30
- }
31
-
32
- if e := err .(* Err ); ! reflect .DeepEqual (e .Data , data ) {
33
- t .Errorf (`wrong data, got %+v, expected %+v` , e .Data , data )
34
- return
35
- }
52
+ func TestErrord (t * testing.T ) {
53
+ t .Run ("when Errord is provided with a message and data, it should create a new error with the message and data" , func (t * testing.T ) {
54
+ msg := "error message"
55
+ data := Data {
56
+ "id" : 1 ,
57
+ "description" : "fool" ,
58
+ }
59
+
60
+ err := Errord (data , msg )
61
+ if got := err .Error (); got != msg {
62
+ t .Errorf (`wrong error message, got "%v", expected "%v"` , got , msg )
63
+ return
64
+ }
65
+
66
+ if e := err .(* Err ); ! reflect .DeepEqual (e .Data , data ) {
67
+ t .Errorf (`wrong data, got "%+v", expected "%+v"` , e .Data , data )
68
+ return
69
+ }
70
+ })
36
71
}
37
72
38
73
func TestWrap (t * testing.T ) {
39
- msg1 := "error message 1"
40
- err1 := New (msg1 )
41
- msg2 := "error message 2"
42
- err2 := Wrap (err1 , msg2 )
43
- msg3 := "error message 3"
44
- err3 := Wrap (err2 , msg3 )
45
- got := err3 .Error ()
46
- expected := fmt .Sprintf ("%s: %s: %s" , msg3 , msg2 , msg1 )
47
- if got != expected {
48
- t .Errorf (`wrong error message, got "%s", expected "%s"` , got , expected )
49
- return
50
- }
74
+ t .Run ("when Wrap is provided with an error and a message, it should create a new error with the message and the provided error as the cause" , func (t * testing.T ) {
75
+ msg1 := "error message 1"
76
+ err1 := New (msg1 )
77
+ msg2 := "error message 2"
78
+ err2 := Wrap (err1 , msg2 )
79
+ msg3 := "error message 3"
80
+ err3 := Wrap (err2 , msg3 )
81
+ got := err3 .Error ()
82
+ expected := fmt .Sprintf ("%s: %s: %s" , msg3 , msg2 , msg1 )
83
+ if got != expected {
84
+ t .Errorf (`wrong error message, got "%s", expected "%s"` , got , expected )
85
+ return
86
+ }
87
+ })
51
88
}
52
89
53
90
func TestWrapc (t * testing.T ) {
54
- msg1 := "error message 1"
55
- err1 := errors .New (msg1 )
56
-
57
- msg2 := "error message 2"
58
- data2 := Data {
59
- "id" : 2 ,
60
- "description" : "bar" ,
61
- }
62
- err2 := Wrapd (err1 , data2 , msg2 )
63
-
64
- msg3 := "error message 3"
65
- data3 := Data {
66
- "id" : 3 ,
67
- "description" : "spam" ,
68
- }
69
- err3 := Wrapd (err2 , data3 , msg3 )
70
-
71
- msg4 := "error message 4"
72
- data4 := Data {
73
- "id" : 4 ,
74
- "description" : "spam" ,
75
- }
76
- err4 := Wrapd (err3 , data4 , msg4 )
77
-
78
- got := err4 .Error ()
79
- expected := fmt .Sprintf ("%s: %s: %s: %s" , msg4 , msg3 , msg2 , msg1 )
80
- if got != expected {
81
- t .Errorf (`wrong error message, got "%s", expected "%s"` , got , expected )
82
- return
83
- }
84
- }
91
+ t .Run ("when Wrapd is provided with an error and data, it should add the data to the error" , func (t * testing.T ) {
92
+ msg1 := "error message 1"
93
+ err1 := errors .New (msg1 )
94
+
95
+ msg2 := "error message 2"
96
+ data2 := Data {
97
+ "id" : 2 ,
98
+ "description" : "bar" ,
99
+ }
100
+ err2 := Wrapd (err1 , data2 , msg2 )
85
101
86
- // CustomError is a custom error type composed with Err.
87
- type CustomError struct {
88
- * Err
89
- }
102
+ msg3 := "error message 3"
103
+ data3 := Data {
104
+ "id" : 3 ,
105
+ "description" : "spam" ,
106
+ }
107
+ err3 := Wrapd (err2 , data3 , msg3 )
90
108
91
- // NewCustomError returns a new CustomError and adds a stack trace.
92
- func NewCustomError (message string ) error {
93
- customError := CustomError {Err : & Err {Message : message }}
94
- WithStack (customError .Err )
95
- return customError
109
+ msg4 := "error message 4"
110
+ data4 := Data {
111
+ "id" : 4 ,
112
+ "description" : "spam" ,
113
+ }
114
+ err4 := Wrapd (err3 , data4 , msg4 )
115
+
116
+ got := err4 .Error ()
117
+ expected := fmt .Sprintf ("%s: %s: %s: %s" , msg4 , msg3 , msg2 , msg1 )
118
+ if got != expected {
119
+ t .Errorf (`wrong error message, got "%s", expected "%s"` , got , expected )
120
+ return
121
+ }
122
+
123
+ if e := err4 .(* Err ); ! reflect .DeepEqual (e .Data , data4 ) {
124
+ t .Errorf (`wrong data, got "%+v", expected "%+v"` , e .Data , data4 )
125
+ return
126
+ }
127
+
128
+ if e := err3 .(* Err ); ! reflect .DeepEqual (e .Data , data3 ) {
129
+ t .Errorf (`wrong data, got "%+v", expected "%+v"` , e .Data , data3 )
130
+ return
131
+ }
132
+
133
+ if e := err2 .(* Err ); ! reflect .DeepEqual (e .Data , data2 ) {
134
+ t .Errorf (`wrong data, got "%+v", expected "%+v"` , e .Data , data2 )
135
+ return
136
+ }
137
+ })
96
138
}
97
139
98
140
func TestWithStack (t * testing.T ) {
99
141
t .Run ("when WithStack is provided with an error of type Err, it should add a stack trace to the error" , func (t * testing.T ) {
100
142
err := NewCustomError ("this is a custom error type with stack" )
101
143
102
144
if err .(CustomError ).Stack == nil {
103
- t .Errorf ( ` expected stack to be not nil, got nil` )
145
+ t .Fatal ( " expected stack to be not nil, got nil" )
104
146
return
105
147
}
106
148
107
149
outputStr := fmt .Sprintf ("%+v" , err )
108
150
if ! strings .Contains (outputStr , "message:" ) {
109
- t .Errorf (`expected "message:" to be in the output string, got %v` , outputStr )
110
- return
151
+ t .Errorf (`expected "message:" to be in the output string, got "%v"` , outputStr )
111
152
}
112
153
if ! strings .Contains (outputStr , "stack:" ) {
113
- t .Errorf (`expected "stack:" to be in the output string, got %v` , outputStr )
114
- return
154
+ t .Errorf (`expected "stack:" to be in the output string, got "%v"` , outputStr )
115
155
}
116
156
})
117
157
}
118
158
119
- // CustomError2 is a custom error type composed with Err.
120
- type CustomError2 struct {
121
- * Err
122
- }
123
-
124
- // NewCustom2Error returns a new CustomError2 and adds a cause to the error.
125
- func NewCustom2Error (message string , cause error ) error {
126
- customError2 := CustomError2 {Err : & Err {Message : message }}
127
- WithCause (customError2 .Err , cause )
128
- return customError2
129
- }
130
-
131
159
func TestWithCause (t * testing.T ) {
132
160
t .Run ("when WithCause is provided with an error and a cause, it should add the cause to the error" , func (t * testing.T ) {
133
161
causeErr := New ("inner error" )
134
162
err := NewCustom2Error ("outer error" , causeErr )
135
163
136
164
if err .(CustomError2 ).Cause != causeErr {
137
- t .Errorf (`expected cause to be %v, got %v` , causeErr , err .(CustomError2 ).Cause )
165
+ t .Errorf (`expected cause to be "%v", got "%v"` , causeErr , err .(CustomError2 ).Cause )
166
+ }
167
+ })
168
+ }
169
+
170
+ func TestIsErrComposition (t * testing.T ) {
171
+ t .Run ("when a custom error type is composed with *Err, it should return true" , func (t * testing.T ) {
172
+ err := NewCustomError ("this is a custom error type with stack" )
173
+ if ! IsErrComposition (err ) {
174
+ t .Errorf ("expected IsErrComposition to return true, got false" )
175
+ }
176
+ })
177
+
178
+ t .Run ("when an error type is Pointer but the element type is a struct not composed with *Err, it should return false" , func (t * testing.T ) {
179
+ err := errors .New ("this is a regular error" )
180
+ if IsErrComposition (err ) {
181
+ t .Errorf ("expected IsErrComposition to return false, got true" )
182
+ }
183
+ })
184
+
185
+ t .Run ("when a custom error type is not composed with *Err, it should return false" , func (t * testing.T ) {
186
+ err := customError3 {}
187
+ if IsErrComposition (err ) {
188
+ t .Errorf ("expected IsErrComposition to return false, got true" )
138
189
}
139
190
})
140
191
}
0 commit comments