File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -609,7 +609,12 @@ def handle_error(self, e):
609
609
'''
610
610
got_request_exception .send (current_app ._get_current_object (), exception = e )
611
611
612
- if not isinstance (e , HTTPException ) and current_app .propagate_exceptions :
612
+ # When propagate_exceptions is set, do not return the exception to the
613
+ # client if a handler is configured for the exception.
614
+ if not isinstance (e , HTTPException ) and \
615
+ current_app .propagate_exceptions and \
616
+ not isinstance (e , tuple (self .error_handlers .keys ())):
617
+
613
618
exc_type , exc_value , tb = sys .exc_info ()
614
619
if exc_value is e :
615
620
raise
Original file line number Diff line number Diff line change @@ -637,3 +637,28 @@ def handle_custom_exception(error):
637
637
'$ref' : '#/responses/CustomException'
638
638
}
639
639
}
640
+
641
+ def test_errorhandler_with_propagate_true (self , app , client ):
642
+ '''Exceptions with errorhandler should not be returned to client, even
643
+ if PROPAGATE_EXCEPTIONS is set.'''
644
+ app .config ['PROPAGATE_EXCEPTIONS' ] = True
645
+ api = restplus .Api (app )
646
+
647
+ @api .route ('/test/' , endpoint = 'test' )
648
+ class TestResource (restplus .Resource ):
649
+ def get (self ):
650
+ raise RuntimeError ('error' )
651
+
652
+ @api .errorhandler (RuntimeError )
653
+ def handle_custom_exception (error ):
654
+ return {'message' : str (error ), 'test' : 'value' }, 400
655
+
656
+ response = client .get ('/test/' )
657
+ assert response .status_code == 400
658
+ assert response .content_type == 'application/json'
659
+
660
+ data = json .loads (response .data .decode ('utf8' ))
661
+ assert data == {
662
+ 'message' : 'error' ,
663
+ 'test' : 'value' ,
664
+ }
You can’t perform that action at this time.
0 commit comments