diff --git a/bumper/web/middlewares.py b/bumper/web/middlewares.py index 0482069..0f4b127 100644 --- a/bumper/web/middlewares.py +++ b/bumper/web/middlewares.py @@ -47,43 +47,50 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse } try: - if request.content_length: - if request.content_type == "application/json": - to_log["request"]["body"] = await request.json() - else: - to_log["request"]["body"] = {h for h in await request.post()} + try: + if request.content_length: + if request.content_type == "application/json": + to_log["request"]["body"] = await request.json() + else: + to_log["request"]["body"] = {h for h in await request.post()} + except Exception: + _LOGGER.exception( + "An exception occurred during logging the request.", exc_info=True + ) + raise response = await handler(request) - if response is None: - _LOGGER.warning( # type:ignore[unreachable] - "Response was null!" + + try: + if response is None: + _LOGGER.warning( # type:ignore[unreachable] + "Response was null!" + ) + _LOGGER.warning(json.dumps(to_log, cls=CustomEncoder)) + raise HTTPNoContent + + to_log["response"] = { + "status": f"{response.status}", + "headers": {h for h in response.headers.items()}, + } + + if isinstance(response, Response) and response.body: + assert response.text + if response.content_type == "application/json": + to_log["response"]["body"] = json.loads(response.text) + elif response.content_type.startswith("text"): + to_log["response"]["body"] = response.text + + return response + except Exception: + _LOGGER.exception( + "An exception occurred during logging the response", exc_info=True ) - _LOGGER.warning(json.dumps(to_log, cls=CustomEncoder)) - raise HTTPNoContent - - to_log["response"] = { - "status": f"{response.status}", - "headers": {h for h in response.headers.items()}, - } - - if isinstance(response, Response) and response.body: - assert response.text - if response.content_type == "application/json": - to_log["response"]["body"] = json.loads(response.text) - elif response.content_type.startswith("text"): - to_log["response"]["body"] = response.text - - return response + raise except web.HTTPNotFound: _LOGGER.debug(f"Request path {request.raw_path} not found") raise - except Exception: - _LOGGER.exception( - "An exception occurred in the logging middleware.", exc_info=True - ) - raise - finally: _LOGGER.debug(json.dumps(to_log, cls=CustomEncoder)) diff --git a/bumper/web/server.py b/bumper/web/server.py index 6144d5d..8e540e9 100644 --- a/bumper/web/server.py +++ b/bumper/web/server.py @@ -333,10 +333,8 @@ class WebServer: ) else: # handle json - jdata = read_body.decode("utf8") - jdata = json.loads(jdata) async with session.request( - request.method, request.url, json=jdata + request.method, request.url, json=request.json() ) as resp: response = await resp.text() proxymodelog.info(