diff --git a/falcon/app_helpers.py b/falcon/app_helpers.py index 0f9893288..5f34bbed5 100644 --- a/falcon/app_helpers.py +++ b/falcon/app_helpers.py @@ -316,7 +316,7 @@ def default_serialize_error(req: Request, resp: Response, exception: HTTPError) preferred = req.client_prefers(list(resp.options.media_handlers)) if preferred is not None: - handler, serialize_sync, _ = resp.options.media_handlers._resolve( + handler, _, _ = resp.options.media_handlers._resolve( preferred, MEDIA_JSON, raise_not_found=False ) if preferred == MEDIA_JSON: @@ -325,12 +325,10 @@ def default_serialize_error(req: Request, resp: Response, exception: HTTPError) # media_handlers. resp.data = exception.to_json(handler) elif handler: - if serialize_sync: - resp.data = serialize_sync(exception.to_dict(), preferred) - else: - # NOTE(caselit): Let the app serialize the response if there is no sync - # serializer implemented in the handler. - resp.media = exception.to_dict() + # NOTE(caselit): Let the app serialize the response even if it needs + # to re-get the handler, since async handlers may not have a sync + # version available. + resp.media = exception.to_dict() else: resp.data = exception.to_xml() diff --git a/falcon/asgi/app.py b/falcon/asgi/app.py index d28c8c49a..dca7afd96 100644 --- a/falcon/asgi/app.py +++ b/falcon/asgi/app.py @@ -566,7 +566,9 @@ async def __call__( # type: ignore[override] # noqa: C901 ) if serialize_sync: - resp._media_rendered = serialize_sync(resp._media) + resp._media_rendered = serialize_sync( + resp._media, resp.content_type + ) else: resp._media_rendered = await handler.serialize_async( resp._media, resp.content_type diff --git a/tests/test_httperror.py b/tests/test_httperror.py index ba368f186..093df6c54 100644 --- a/tests/test_httperror.py +++ b/tests/test_httperror.py @@ -972,19 +972,19 @@ def test_kw_only(self): class FakeYamlMediaHandler(BaseHandler): - def serialize(self, media, content_type): + def serialize(self, media, content_type=None): assert media == {'title': '410 Gone'} return b'title: 410 Gone!' class AsyncOnlyMediaHandler(BaseHandler): - async def serialize_async(self, media, content_type): + async def serialize_async(self, media, content_type=None): assert media == {'title': '410 Gone'} return b'this is async' class SyncInterfaceMediaHandler(AsyncOnlyMediaHandler): - def serialize(self, media, content_type): + def serialize(self, media, content_type=None): assert media == {'title': '410 Gone'} return b'this is sync instead'