@@ -27,6 +27,22 @@ func TestErrToStatusCode(t *testing.T) {
27
27
}
28
28
}
29
29
30
+ // TestWrappedErrToStatusCode ensures that the middleware also works with wrapped errors.
31
+ func TestWrappedErrToStatusCode (t * testing.T ) {
32
+ r := gin .Default ()
33
+ r .Use (Error (NewErrMap (BadRequestErr ).StatusCode (http .StatusBadRequest )))
34
+ r .GET ("/test" , func (c * gin.Context ) {
35
+ _ = c .Error (fmt .Errorf ("%w: this is a wrapped error" , BadRequestErr ))
36
+ })
37
+
38
+ recorder := httptest .NewRecorder ()
39
+ r .ServeHTTP (recorder , httptest .NewRequest ("GET" , "/test" , nil ))
40
+
41
+ if recorder .Result ().StatusCode != http .StatusBadRequest {
42
+ t .Errorf ("invalid the status code %+v" , recorder .Result ().StatusCode )
43
+ }
44
+ }
45
+
30
46
func TestErrToResponse (t * testing.T ) {
31
47
r := gin .Default ()
32
48
r .Use (Error (
@@ -64,3 +80,21 @@ func TestErrToResponse(t *testing.T) {
64
80
t .Errorf ("invalid the response body %+v" , rsp .Error )
65
81
}
66
82
}
83
+
84
+ func TestErrorMap_matchError (t * testing.T ) {
85
+ t .Run ("single error" , func (t * testing.T ) {
86
+ em := NewErrMap (BadRequestErr )
87
+ err := BadRequestErr
88
+ if em .matchError (err ) == false {
89
+ t .Errorf ("error should match" )
90
+ }
91
+ })
92
+
93
+ t .Run ("wrapped error" , func (t * testing.T ) {
94
+ em := NewErrMap (BadRequestErr )
95
+ err := fmt .Errorf ("%w: Details about this error" , BadRequestErr )
96
+ if em .matchError (err ) == false {
97
+ t .Errorf ("error should match" )
98
+ }
99
+ })
100
+ }
0 commit comments