Skip to content

Commit

Permalink
chore: simplify code a bit, avoiding unnecessary complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
CaselIT committed Sep 29, 2024
1 parent 29413b8 commit 6472075
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
12 changes: 5 additions & 7 deletions falcon/app_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion falcon/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/test_httperror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 6472075

Please sign in to comment.