Skip to content

Commit

Permalink
Merge pull request #29 from Aksem/feature/web_aiohttp
Browse files Browse the repository at this point in the history
Add content-type to errors in web aiohttp
  • Loading branch information
Aksem authored Oct 17, 2024
2 parents d85dc06 + d9e8565 commit 9d265c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
45 changes: 30 additions & 15 deletions modapp/transports/web_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,43 @@ def _get_cors_headers(cors_allow: str | None) -> dict[str, str]:
return headers


def _exception_to_response(error: Exception, converter: BaseConverter, cors_allow: str | None) -> Exception:
def _exception_to_response(
error: Exception, converter: BaseConverter, cors_allow: str | None
) -> Exception:
if isinstance(error, NotFoundError):
return web.HTTPNotFound(headers=_get_cors_headers(cors_allow), body=converter.error_to_raw(error))
return web.HTTPNotFound(
headers=_get_cors_headers(cors_allow),
body=converter.error_to_raw(error),
content_type=_get_content_type(converter),
)
elif isinstance(error, InvalidArgumentError):
return web.HTTPUnprocessableEntity(headers=_get_cors_headers(cors_allow), body=converter.error_to_raw(error))
return web.HTTPUnprocessableEntity(
headers=_get_cors_headers(cors_allow),
body=converter.error_to_raw(error),
content_type=_get_content_type(converter),
)
elif isinstance(error, ServerError):
# does the same as return statement below, but shows explicitly mapping of ServerError to
# http error
return web.HTTPInternalServerError(headers=_get_cors_headers(cors_allow), body=converter.error_to_raw(error))
return web.HTTPInternalServerError(
headers=_get_cors_headers(cors_allow),
body=converter.error_to_raw(error),
content_type=_get_content_type(converter),
)

return web.HTTPInternalServerError(headers=_get_cors_headers(cors_allow))


def _get_content_type(converter: BaseConverter) -> str:
if JsonConverter is not None and isinstance(converter, JsonConverter):
content_type = "application/json"
elif ProtobufConverter is not None and isinstance(converter, ProtobufConverter):
content_type = "application/octet-stream"
else:
content_type = ""
return content_type


async def route_handler(
route: Route, request: web.Request, transport: WebAiohttpTransport
) -> web.Response:
Expand All @@ -68,20 +92,11 @@ async def route_handler(
except Exception as error:
raise _exception_to_response(error, transport.converter, cors_allow)

if JsonConverter is not None and isinstance(transport.converter, JsonConverter):
content_type = "application/json"
elif ProtobufConverter is not None and isinstance(
transport.converter, ProtobufConverter
):
content_type = "application/octet-stream"
else:
content_type = ""

return web.Response(
body=result,
status=201,
headers=_get_cors_headers(cors_allow),
content_type=content_type,
content_type=_get_content_type(transport.converter),
)
# elif route.proto_cardinality == Cardinality.UNARY_STREAM:
# conn_id = request.get_header("connection-id")
Expand Down Expand Up @@ -216,7 +231,7 @@ async def start(self, routes: RoutesDict) -> None:
static_dir_index_handler, static_dir_path=static_dir_path
),
),
web.static(static_dir_route, static_dir_path)
web.static(static_dir_route, static_dir_path),
]
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "modapp"
version = "0.3.19"
version = "0.3.20"
description = ""
authors = ["Vladyslav Hnatiuk <vhnatiuk@ukr.net>"]
license = "MIT"
Expand Down

0 comments on commit 9d265c2

Please sign in to comment.