Skip to content

Commit eb7a786

Browse files
authored
fix(error-reporter): do not report the same derived exception twice (#2748)
* fix(error-reporting): add a failing regression test * fix(error-reporter): do not report the same derived HTTPError twice
1 parent f3ac839 commit eb7a786

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎falcon/app.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ def _handle_exception(
14221422
# below, so they are all reported as unhandled.
14231423
if self._report_error is not None:
14241424
self._report_error(req, ex, params, False)
1425-
if derived_ex is not None:
1425+
if derived_ex is not None and derived_ex is not ex:
14261426
self._report_error(req, derived_ex, params, False)
14271427
if handler_ex is not ex:
14281428
self._report_error(req, handler_ex, params, False)

‎falcon/asgi/app.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ async def _handle_exception( # type: ignore[override]
14751475
# below, so they are all reported as unhandled.
14761476
if self._report_error is not None:
14771477
self._report_error(req, ex, params, False)
1478-
if derived_ex is not None:
1478+
if derived_ex is not None and derived_ex is not ex:
14791479
self._report_error(req, derived_ex, params, False)
14801480
if handler_ex is not ex:
14811481
self._report_error(req, handler_ex, params, False)

‎tests/test_error_reporter.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ def bubble_up(req, resp, ex, params):
126126
assert reporter.log == [(falcon.HTTPRouteNotFound, True)]
127127

128128

129+
@pytest.mark.filterwarnings('ignore:Unknown REQUEST_METHOD')
130+
def test_handler_reraises_serializer_raises(asgi, client, reporter):
131+
def bubble_up(req, resp, ex, params):
132+
raise
133+
134+
def serialize_error(req, resp, exception):
135+
resp.media = {'inverse_client_code': 1.0 / (exception.status_code - 400)}
136+
137+
client.app.add_error_handler(falcon.HTTPError, _error_handler(asgi, bubble_up))
138+
client.app.set_error_serializer(serialize_error)
139+
140+
with pytest.raises(ZeroDivisionError):
141+
client.request(method='WEBSOCKET')
142+
143+
assert reporter.log == [(falcon.HTTPBadRequest, False), (ZeroDivisionError, False)]
144+
145+
129146
def test_handler_raises_http_status(asgi, client, reporter):
130147
def handle_zero_division(req, resp, ex, params):
131148
raise falcon.HTTPStatus(falcon.HTTP_OK, text='{"result": null}')

0 commit comments

Comments
 (0)