Skip to content

Commit fe9fec6

Browse files
authored
Merge pull request #706 from rob-smallshire/error-handler-suppression
Fix error handler exception suppression
2 parents 96f60af + f1ecb4a commit fe9fec6

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Changelog
66
Current
77
-------
88

9-
- Nothing yet
9+
- Ensure that exceptions raised in error handler, including programming errors, are logged (:issue:`705`, :pr:`706`)
1010

1111
0.13.0 (2019-08-12)
1212
-------------------

flask_restplus/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,8 @@ def error_router(self, original_handler, e):
579579
if self._has_fr_route():
580580
try:
581581
return self.handle_error(e)
582-
except Exception:
583-
pass # Fall through to original handler
582+
except Exception as f:
583+
return original_handler(f)
584584
return original_handler(e)
585585

586586
def handle_error(self, e):

tests/test_errors.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from __future__ import unicode_literals
33

44
import json
5+
import logging
6+
57
import pytest
68

79
from flask import Blueprint, abort
@@ -162,6 +164,31 @@ def handle_custom_exception(error):
162164
'test': 'value',
163165
}
164166

167+
def test_blunder_in_errorhandler_is_not_suppressed_in_logs(self, app, client, caplog):
168+
169+
api = restplus.Api(app)
170+
171+
class CustomException(RuntimeError):
172+
pass
173+
174+
class ProgrammingBlunder(Exception):
175+
pass
176+
177+
@api.route('/test/', endpoint="test")
178+
class TestResource(restplus.Resource):
179+
def get(self):
180+
raise CustomException('error')
181+
182+
@api.errorhandler(CustomException)
183+
def handle_custom_exception(error):
184+
raise ProgrammingBlunder("This exception needs to be logged, not suppressed, then cause 500")
185+
186+
with caplog.at_level(logging.ERROR):
187+
response = client.get('/test/')
188+
exc_type, value, traceback = caplog.records[0].exc_info
189+
assert exc_type is ProgrammingBlunder
190+
assert response.status_code == 500
191+
165192
def test_errorhandler_for_custom_exception_with_headers(self, app, client):
166193
api = restplus.Api(app)
167194

@@ -480,15 +507,23 @@ def test_handle_not_include_error_message(self, app):
480507
assert 'message' not in json.loads(response.data.decode())
481508

482509
def test_error_router_falls_back_to_original(self, app, mocker):
510+
class ProgrammingBlunder(Exception):
511+
pass
512+
513+
blunder = ProgrammingBlunder("This exception needs to be detectable")
514+
515+
def raise_blunder(arg):
516+
raise blunder
517+
483518
api = restplus.Api(app)
484519
app.handle_exception = mocker.Mock()
485-
api.handle_error = mocker.Mock(side_effect=Exception())
520+
api.handle_error = mocker.Mock(side_effect=raise_blunder)
486521
api._has_fr_route = mocker.Mock(return_value=True)
487522
exception = mocker.Mock(spec=HTTPException)
488523

489524
api.error_router(app.handle_exception, exception)
490525

491-
app.handle_exception.assert_called_with(exception)
526+
app.handle_exception.assert_called_with(blunder)
492527

493528
def test_fr_405(self, app, client):
494529
api = restplus.Api(app)

0 commit comments

Comments
 (0)