@@ -29,20 +29,46 @@ func TestMap_CustomType_Echo(t *testing.T) {
29
29
30
30
err := echo_endpoint1 (c )
31
31
32
- var problemErr ProblemDetailErr
33
-
34
32
Map [custom_errors.BadRequestError ](func () ProblemDetailErr {
35
- problemErr = New (http .StatusBadRequest , "bad-request" , err .Error ())
36
- return problemErr
33
+ return New (http .StatusBadRequest , "bad-request" , err .Error ())
37
34
})
38
35
39
- _ = ResolveProblemDetails (c .Response (), c .Request (), err )
36
+ p , _ : = ResolveProblemDetails (c .Response (), c .Request (), err )
40
37
41
38
assert .Equal (t , c .Response ().Status , http .StatusBadRequest )
42
- assert .Equal (t , err .Error (), problemErr .GetDetails ())
43
- assert .Equal (t , "bad-request" , problemErr .GetTitle ())
44
- assert .Equal (t , "https://httpstatuses.io/400" , problemErr .GetType ())
45
- assert .Equal (t , http .StatusBadRequest , problemErr .GetStatus ())
39
+ assert .Equal (t , err .Error (), p .GetDetails ())
40
+ assert .Equal (t , "bad-request" , p .GetTitle ())
41
+ assert .Equal (t , "https://httpstatuses.io/400" , p .GetType ())
42
+ assert .Equal (t , http .StatusBadRequest , p .GetStatus ())
43
+ }
44
+
45
+ func TestMap_Custom_Problem_Err_Echo (t * testing.T ) {
46
+
47
+ e := echo .New ()
48
+ req := httptest .NewRequest (http .MethodGet , "http://echo_endpoint4" , nil )
49
+ rec := httptest .NewRecorder ()
50
+ c := e .NewContext (req , rec )
51
+
52
+ err := echo_endpoint4 (c )
53
+
54
+ Map [custom_errors.ConflictError ](func () ProblemDetailErr {
55
+ return & CustomProblemDetailTest {
56
+ ProblemDetailErr : New (http .StatusConflict , "conflict" , err .Error ()),
57
+ AdditionalInfo : "some additional info..." ,
58
+ Description : "some description..." ,
59
+ }
60
+ })
61
+
62
+ p , _ := ResolveProblemDetails (c .Response (), c .Request (), err )
63
+ cp := p .(* CustomProblemDetailTest )
64
+
65
+ assert .Equal (t , c .Response ().Status , http .StatusConflict )
66
+ assert .Equal (t , err .Error (), cp .GetDetails ())
67
+ assert .Equal (t , "conflict" , cp .GetTitle ())
68
+ assert .Equal (t , "https://httpstatuses.io/409" , cp .GetType ())
69
+ assert .Equal (t , http .StatusConflict , cp .GetStatus ())
70
+ assert .Equal (t , "some description..." , cp .Description )
71
+ assert .Equal (t , "some additional info..." , cp .AdditionalInfo )
46
72
}
47
73
48
74
func TestMap_Status_Echo (t * testing.T ) {
@@ -54,21 +80,35 @@ func TestMap_Status_Echo(t *testing.T) {
54
80
55
81
err := echo_endpoint2 (c )
56
82
57
- var problemErr ProblemDetailErr
58
-
59
- // map status code to problem details error
60
83
MapStatus (http .StatusBadGateway , func () ProblemDetailErr {
61
- problemErr = New (http .StatusUnauthorized , "unauthorized" , err .Error ())
62
- return problemErr
84
+ return New (http .StatusUnauthorized , "unauthorized" , err .Error ())
63
85
})
64
86
65
- _ = ResolveProblemDetails (c .Response (), c .Request (), err )
87
+ p , _ : = ResolveProblemDetails (c .Response (), c .Request (), err )
66
88
67
89
assert .Equal (t , c .Response ().Status , http .StatusUnauthorized )
68
- assert .Equal (t , err .(* echo.HTTPError ).Message .(error ).Error (), problemErr .GetDetails ())
69
- assert .Equal (t , "unauthorized" , problemErr .GetTitle ())
70
- assert .Equal (t , "https://httpstatuses.io/401" , problemErr .GetType ())
71
- assert .Equal (t , http .StatusUnauthorized , problemErr .GetStatus ())
90
+ assert .Equal (t , err .(* echo.HTTPError ).Message .(error ).Error (), p .GetDetails ())
91
+ assert .Equal (t , "unauthorized" , p .GetTitle ())
92
+ assert .Equal (t , "https://httpstatuses.io/401" , p .GetType ())
93
+ assert .Equal (t , http .StatusUnauthorized , p .GetStatus ())
94
+ }
95
+
96
+ func TestMap_Unhandled_Err_Echo (t * testing.T ) {
97
+
98
+ e := echo .New ()
99
+ req := httptest .NewRequest (http .MethodGet , "http://echo_endpoint3" , nil )
100
+ rec := httptest .NewRecorder ()
101
+ c := e .NewContext (req , rec )
102
+
103
+ err := echo_endpoint3 (c )
104
+
105
+ p , _ := ResolveProblemDetails (c .Response (), c .Request (), err )
106
+
107
+ assert .Equal (t , c .Response ().Status , http .StatusInternalServerError )
108
+ assert .Equal (t , err .Error (), p .GetDetails ())
109
+ assert .Equal (t , "Internal Server Error" , p .GetTitle ())
110
+ assert .Equal (t , "https://httpstatuses.io/500" , p .GetType ())
111
+ assert .Equal (t , http .StatusInternalServerError , p .GetStatus ())
72
112
}
73
113
74
114
func TestMap_CustomType_Gin (t * testing.T ) {
@@ -89,19 +129,54 @@ func TestMap_CustomType_Gin(t *testing.T) {
89
129
90
130
for _ , err := range c .Errors {
91
131
92
- var problemErr ProblemDetailErr
93
-
94
132
Map [custom_errors.BadRequestError ](func () ProblemDetailErr {
95
- problemErr = New (http .StatusBadRequest , "bad-request" , err .Error ())
96
- return problemErr
133
+ return New (http .StatusBadRequest , "bad-request" , err .Error ())
134
+ })
135
+
136
+ p , _ := ResolveProblemDetails (w , req , err )
137
+
138
+ assert .Equal (t , http .StatusBadRequest , p .GetStatus ())
139
+ assert .Equal (t , err .Error (), p .GetDetails ())
140
+ assert .Equal (t , "bad-request" , p .GetTitle ())
141
+ assert .Equal (t , "https://httpstatuses.io/400" , p .GetType ())
142
+ }
143
+ }
144
+
145
+ func TestMap_Custom_Problem_Err_Gin (t * testing.T ) {
146
+
147
+ gin .SetMode (gin .TestMode )
148
+ w := httptest .NewRecorder ()
149
+ c , _ := gin .CreateTestContext (w )
150
+ r := gin .Default ()
151
+
152
+ r .GET ("/gin_endpoint4" , func (ctx * gin.Context ) {
153
+ err := errors .New ("We have a custom error with custom problem details error in our endpoint" )
154
+ customConflictError := custom_errors.ConflictError {InternalError : err }
155
+ _ = c .Error (customConflictError )
156
+ })
157
+
158
+ req , _ := http .NewRequest (http .MethodGet , "/gin_endpoint4" , nil )
159
+ r .ServeHTTP (w , req )
160
+
161
+ for _ , err := range c .Errors {
162
+
163
+ Map [custom_errors.ConflictError ](func () ProblemDetailErr {
164
+ return & CustomProblemDetailTest {
165
+ ProblemDetailErr : New (http .StatusConflict , "conflict" , err .Error ()),
166
+ AdditionalInfo : "some additional info..." ,
167
+ Description : "some description..." ,
168
+ }
97
169
})
98
170
99
- _ = ResolveProblemDetails (w , req , err )
171
+ p , _ := ResolveProblemDetails (w , req , err )
172
+ cp := p .(* CustomProblemDetailTest )
100
173
101
- assert .Equal (t , http .StatusBadRequest , problemErr .GetStatus ())
102
- assert .Equal (t , err .Error (), problemErr .GetDetails ())
103
- assert .Equal (t , "bad-request" , problemErr .GetTitle ())
104
- assert .Equal (t , "https://httpstatuses.io/400" , problemErr .GetType ())
174
+ assert .Equal (t , http .StatusConflict , cp .GetStatus ())
175
+ assert .Equal (t , err .Error (), cp .GetDetails ())
176
+ assert .Equal (t , "conflict" , cp .GetTitle ())
177
+ assert .Equal (t , "https://httpstatuses.io/409" , cp .GetType ())
178
+ assert .Equal (t , "some description..." , cp .Description )
179
+ assert .Equal (t , "some additional info..." , cp .AdditionalInfo )
105
180
}
106
181
}
107
182
@@ -114,7 +189,6 @@ func TestMap_Status_Gin(t *testing.T) {
114
189
115
190
r .GET ("/gin_endpoint2" , func (ctx * gin.Context ) {
116
191
err := errors .New ("We have a specific status code error in our endpoint" )
117
- // change status code 'StatusBadGateway' to 'StatusUnauthorized' base on handler config
118
192
_ = c .AbortWithError (http .StatusBadGateway , err )
119
193
})
120
194
@@ -123,20 +197,42 @@ func TestMap_Status_Gin(t *testing.T) {
123
197
124
198
for _ , err := range c .Errors {
125
199
126
- var problemErr ProblemDetailErr
127
-
128
- // map status code to problem details error
129
200
MapStatus (http .StatusBadGateway , func () ProblemDetailErr {
130
- problemErr = New (http .StatusUnauthorized , "unauthorized" , err .Error ())
131
- return problemErr
201
+ return New (http .StatusUnauthorized , "unauthorized" , err .Error ())
132
202
})
133
203
134
- _ = ResolveProblemDetails (w , req , err )
204
+ p , _ := ResolveProblemDetails (w , req , err )
205
+
206
+ assert .Equal (t , http .StatusUnauthorized , p .GetStatus ())
207
+ assert .Equal (t , err .Error (), p .GetDetails ())
208
+ assert .Equal (t , "unauthorized" , p .GetTitle ())
209
+ assert .Equal (t , "https://httpstatuses.io/401" , p .GetType ())
210
+ }
211
+ }
212
+
213
+ func TestMap_Unhandled_Err_Gin (t * testing.T ) {
214
+
215
+ gin .SetMode (gin .TestMode )
216
+ w := httptest .NewRecorder ()
217
+ c , _ := gin .CreateTestContext (w )
218
+ r := gin .Default ()
219
+
220
+ r .GET ("/gin_endpoint3" , func (ctx * gin.Context ) {
221
+ err := errors .New ("We have a unhandeled error in our endpoint" )
222
+ _ = c .Error (err )
223
+ })
224
+
225
+ req , _ := http .NewRequest (http .MethodGet , "/gin_endpoint3" , nil )
226
+ r .ServeHTTP (w , req )
227
+
228
+ for _ , err := range c .Errors {
229
+
230
+ p , _ := ResolveProblemDetails (w , req , err )
135
231
136
- assert .Equal (t , http .StatusUnauthorized , problemErr .GetStatus ())
137
- assert .Equal (t , err .Error (), problemErr .GetDetails ())
138
- assert .Equal (t , "unauthorized " , problemErr .GetTitle ())
139
- assert .Equal (t , "https://httpstatuses.io/401 " , problemErr .GetType ())
232
+ assert .Equal (t , http .StatusInternalServerError , p .GetStatus ())
233
+ assert .Equal (t , err .Error (), p .GetDetails ())
234
+ assert .Equal (t , "Internal Server Error " , p .GetTitle ())
235
+ assert .Equal (t , "https://httpstatuses.io/500 " , p .GetType ())
140
236
}
141
237
}
142
238
@@ -147,6 +243,21 @@ func echo_endpoint1(c echo.Context) error {
147
243
148
244
func echo_endpoint2 (c echo.Context ) error {
149
245
err := errors .New ("We have a specific status code error in our endpoint" )
150
- // change status code 'StatusBadGateway' to 'StatusUnauthorized' base on handler config
151
246
return echo .NewHTTPError (http .StatusBadGateway , err )
152
247
}
248
+
249
+ func echo_endpoint3 (c echo.Context ) error {
250
+ err := errors .New ("We have a unhandeled error in our endpoint" )
251
+ return err
252
+ }
253
+
254
+ func echo_endpoint4 (c echo.Context ) error {
255
+ err := errors .New ("We have a custom error with custom problem details error in our endpoint" )
256
+ return custom_errors.ConflictError {InternalError : err }
257
+ }
258
+
259
+ type CustomProblemDetailTest struct {
260
+ ProblemDetailErr
261
+ Description string `json:"description,omitempty"`
262
+ AdditionalInfo string `json:"additionalInfo,omitempty"`
263
+ }
0 commit comments