Skip to content

Commit de38220

Browse files
brmzkwSteadBytes
authored andcommitted
Don't return exception when errorhandler is set (fix #693) (#741)
If errorhandler is configured, error should not be returned to client even if PROPAGATE_EXCEPTIONS is true.
1 parent 823f61e commit de38220

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

flask_restplus/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,12 @@ def handle_error(self, e):
609609
'''
610610
got_request_exception.send(current_app._get_current_object(), exception=e)
611611

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+
613618
exc_type, exc_value, tb = sys.exc_info()
614619
if exc_value is e:
615620
raise

tests/test_errors.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,28 @@ def handle_custom_exception(error):
637637
'$ref': '#/responses/CustomException'
638638
}
639639
}
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+
}

0 commit comments

Comments
 (0)