Skip to content

Commit

Permalink
Don't return exception when errorhandler is set (fix #693) (#741)
Browse files Browse the repository at this point in the history
If errorhandler is configured, error should not be returned to client
even if PROPAGATE_EXCEPTIONS is true.
  • Loading branch information
brmzkw authored and SteadBytes committed Oct 28, 2019
1 parent 823f61e commit de38220
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion flask_restplus/api.py
Expand Up @@ -609,7 +609,12 @@ def handle_error(self, e):
'''
got_request_exception.send(current_app._get_current_object(), exception=e)

if not isinstance(e, HTTPException) and current_app.propagate_exceptions:
# When propagate_exceptions is set, do not return the exception to the
# client if a handler is configured for the exception.
if not isinstance(e, HTTPException) and \
current_app.propagate_exceptions and \
not isinstance(e, tuple(self.error_handlers.keys())):

exc_type, exc_value, tb = sys.exc_info()
if exc_value is e:
raise
Expand Down
25 changes: 25 additions & 0 deletions tests/test_errors.py
Expand Up @@ -637,3 +637,28 @@ def handle_custom_exception(error):
'$ref': '#/responses/CustomException'
}
}

def test_errorhandler_with_propagate_true(self, app, client):
'''Exceptions with errorhandler should not be returned to client, even
if PROPAGATE_EXCEPTIONS is set.'''
app.config['PROPAGATE_EXCEPTIONS'] = True
api = restplus.Api(app)

@api.route('/test/', endpoint='test')
class TestResource(restplus.Resource):
def get(self):
raise RuntimeError('error')

@api.errorhandler(RuntimeError)
def handle_custom_exception(error):
return {'message': str(error), 'test': 'value'}, 400

response = client.get('/test/')
assert response.status_code == 400
assert response.content_type == 'application/json'

data = json.loads(response.data.decode('utf8'))
assert data == {
'message': 'error',
'test': 'value',
}

0 comments on commit de38220

Please sign in to comment.