|
2 | 2 | from __future__ import unicode_literals
|
3 | 3 |
|
4 | 4 | import json
|
| 5 | +import logging |
| 6 | + |
5 | 7 | import pytest
|
6 | 8 |
|
7 | 9 | from flask import Blueprint, abort
|
@@ -162,6 +164,31 @@ def handle_custom_exception(error):
|
162 | 164 | 'test': 'value',
|
163 | 165 | }
|
164 | 166 |
|
| 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 | + |
165 | 192 | def test_errorhandler_for_custom_exception_with_headers(self, app, client):
|
166 | 193 | api = restplus.Api(app)
|
167 | 194 |
|
@@ -480,15 +507,23 @@ def test_handle_not_include_error_message(self, app):
|
480 | 507 | assert 'message' not in json.loads(response.data.decode())
|
481 | 508 |
|
482 | 509 | 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 | + |
483 | 518 | api = restplus.Api(app)
|
484 | 519 | app.handle_exception = mocker.Mock()
|
485 |
| - api.handle_error = mocker.Mock(side_effect=Exception()) |
| 520 | + api.handle_error = mocker.Mock(side_effect=raise_blunder) |
486 | 521 | api._has_fr_route = mocker.Mock(return_value=True)
|
487 | 522 | exception = mocker.Mock(spec=HTTPException)
|
488 | 523 |
|
489 | 524 | api.error_router(app.handle_exception, exception)
|
490 | 525 |
|
491 |
| - app.handle_exception.assert_called_with(exception) |
| 526 | + app.handle_exception.assert_called_with(blunder) |
492 | 527 |
|
493 | 528 | def test_fr_405(self, app, client):
|
494 | 529 | api = restplus.Api(app)
|
|
0 commit comments