Skip to content

Commit

Permalink
@GitHK review: cleanup middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Oct 21, 2021
1 parent 99e1d73 commit 5d7c2b6
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def is_api_request(request: web.Request, api_version: str) -> bool:

def error_middleware_factory(api_version: str, log_exceptions=True) -> Middleware:

_IS_PROD: bool = is_production_environ()
_is_prod: bool = is_production_environ()

def _process_and_raise_unexpected_error(request: web.BaseRequest, err: Exception):
resp = create_error_response(
err,
"Unexpected Server error",
web.HTTPInternalServerError,
skip_internal_error_details=_IS_PROD,
skip_internal_error_details=_is_prod,
)

if log_exceptions:
Expand Down Expand Up @@ -78,7 +78,7 @@ async def _middleware_handler(request: web.Request, handler: Handler):
except web.HTTPError as err:
# TODO: differenciate between server/client error
if not err.reason:
err.reason = "Unexpected error"
err.set_status(err.status_code, reason="Unexpected error")

err.content_type = JSON_CONTENT_TYPE

Expand Down Expand Up @@ -117,7 +117,7 @@ async def _middleware_handler(request: web.Request, handler: Handler):
err,
str(err),
web.HTTPNotImplemented,
skip_internal_error_details=_IS_PROD,
skip_internal_error_details=_is_prod,
)
raise error_response from err

Expand Down Expand Up @@ -198,11 +198,10 @@ async def _middleware_handler(
assert isinstance(resp, StreamResponse) # nosec
return resp

# WARNING: this is not a handler in the classic way
# NOTE: the return values of this handler
resp: _ResponseOrBodyData = await handler(request)

if isinstance(resp, web.FileResponse):
# WARNING: allows for files to be downloaded
return resp

if not isinstance(resp, StreamResponse):
Expand Down

0 comments on commit 5d7c2b6

Please sign in to comment.