@@ -289,14 +289,19 @@ def test_value_error_raised_if_parse_called_with_invalid_location(parser, web_re
289289
290290@mock .patch ("webargs.core.Parser.handle_error" )
291291def test_handle_error_called_when_parsing_raises_error (handle_error , web_request ):
292+ # handle_error must raise an error to be valid
293+ handle_error .side_effect = ValidationError ("parsing failed" )
294+
292295 def always_fail (* args , ** kwargs ):
293296 raise ValidationError ("error occurred" )
294297
295298 p = Parser ()
296299 assert handle_error .call_count == 0
297- p .parse ({"foo" : fields .Field ()}, web_request , validate = always_fail )
300+ with pytest .raises (ValidationError ):
301+ p .parse ({"foo" : fields .Field ()}, web_request , validate = always_fail )
298302 assert handle_error .call_count == 1
299- p .parse ({"foo" : fields .Field ()}, web_request , validate = always_fail )
303+ with pytest .raises (ValidationError ):
304+ p .parse ({"foo" : fields .Field ()}, web_request , validate = always_fail )
300305 assert handle_error .call_count == 2
301306
302307
@@ -360,6 +365,25 @@ def handle_error(error, req, schema, *, error_status_code, error_headers):
360365 parser .parse (mock_schema , web_request )
361366
362367
368+ def test_custom_error_handler_must_reraise (web_request ):
369+ class CustomError (Exception ):
370+ pass
371+
372+ mock_schema = mock .Mock (spec = Schema )
373+ mock_schema .strict = True
374+ mock_schema .load .side_effect = ValidationError ("parsing json failed" )
375+ parser = Parser ()
376+
377+ @parser .error_handler
378+ def handle_error (error , req , schema , * , error_status_code , error_headers ):
379+ pass
380+
381+ # because the handler above does not raise a new error, the parser should
382+ # raise a ValueError -- indicating a programming error
383+ with pytest .raises (ValueError ):
384+ parser .parse (mock_schema , web_request )
385+
386+
363387def test_custom_location_loader (web_request ):
364388 web_request .data = {"foo" : 42 }
365389
0 commit comments