File tree Expand file tree Collapse file tree 4 files changed +64
-0
lines changed Expand file tree Collapse file tree 4 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,11 @@ func init() {
42
42
var _ http.Handler = & Webhook {}
43
43
44
44
func (wh * Webhook ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
45
+ defer utilruntime .HandleCrash (func (_ interface {}) {
46
+ // Assume the crash happened before the response was written.
47
+ http .Error (w , "internal server error" , http .StatusInternalServerError )
48
+ })
49
+
45
50
ctx := r .Context ()
46
51
if wh .WithContextFunc != nil {
47
52
ctx = wh .WithContextFunc (ctx , r )
Original file line number Diff line number Diff line change @@ -198,6 +198,33 @@ var _ = Describe("Admission Webhooks", func() {
198
198
return respRecorder .Body .Len ()
199
199
}, time .Second * 3 ).Should (Equal (0 ))
200
200
})
201
+
202
+ It ("should handle crashes" , func () {
203
+ req := & http.Request {
204
+ Header : http.Header {"Content-Type" : []string {"application/json" }},
205
+ Method : http .MethodPost ,
206
+ Body : nopCloser {Reader : bytes .NewBufferString (`{"spec":{"token":"foobar"}}` )},
207
+ }
208
+ webhook := & Webhook {
209
+ Handler : & fakeHandler {
210
+ fn : func (ctx context.Context , req Request ) Response {
211
+ panic ("boom" )
212
+ },
213
+ },
214
+ }
215
+
216
+ expected := `internal server error
217
+ `
218
+ (func () {
219
+ defer func () {
220
+ if r := recover (); r != nil {
221
+ Expect (r ).To (Equal ("boom" ))
222
+ }
223
+ }()
224
+ webhook .ServeHTTP (respRecorder , req )
225
+ })()
226
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
227
+ })
201
228
})
202
229
})
203
230
Original file line number Diff line number Diff line change @@ -42,6 +42,11 @@ func init() {
42
42
var _ http.Handler = & Webhook {}
43
43
44
44
func (wh * Webhook ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
45
+ defer utilruntime .HandleCrash (func (_ interface {}) {
46
+ // Assume the crash happened before the response was written.
47
+ http .Error (w , "internal server error" , http .StatusInternalServerError )
48
+ })
49
+
45
50
ctx := r .Context ()
46
51
if wh .WithContextFunc != nil {
47
52
ctx = wh .WithContextFunc (ctx , r )
Original file line number Diff line number Diff line change @@ -181,6 +181,33 @@ var _ = Describe("Authentication Webhooks", func() {
181
181
webhook .ServeHTTP (respRecorder , req .WithContext (ctx ))
182
182
Expect (respRecorder .Body .String ()).To (Equal (expected ))
183
183
})
184
+
185
+ It ("should handle crashes" , func () {
186
+ req := & http.Request {
187
+ Header : http.Header {"Content-Type" : []string {"application/json" }},
188
+ Method : http .MethodPost ,
189
+ Body : nopCloser {Reader : bytes .NewBufferString (`{"spec":{"token":"foobar"}}` )},
190
+ }
191
+ webhook := & Webhook {
192
+ Handler : & fakeHandler {
193
+ fn : func (ctx context.Context , req Request ) Response {
194
+ panic ("boom" )
195
+ },
196
+ },
197
+ }
198
+
199
+ expected := `internal server error
200
+ `
201
+ (func () {
202
+ defer func () {
203
+ if r := recover (); r != nil {
204
+ Expect (r ).To (Equal ("boom" ))
205
+ }
206
+ }()
207
+ webhook .ServeHTTP (respRecorder , req )
208
+ })()
209
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
210
+ })
184
211
})
185
212
})
186
213
You can’t perform that action at this time.
0 commit comments