litestar
litestar copied to clipboard
Enhancement: allow async exception handlers
Summary
I want to log my exceptions, but also log the request headers and body when an exception occurs. Basically what LoggingMiddlewareConfig does, but only when an exception occurs.
Basic Example
I tried to go the route of the example here - https://github.com/litestar-org/litestar-pg-redis-docker/blob/78094bc4ea82839ba8703a41381da1e60f0b1d4d/app/core/exceptions.py Here's my code
async def logging_exception_handler(request: Request, exc: Exception) -> Response[Any]:
logging_middleware_config = LoggingMiddlewareConfig()
extracted_data = await logging_middleware_config.middleware_class.extract_request_data(request)
logging_middleware_config.middleware_class.log_message(values=extracted_data)
logger.error("Application Exception", exc_info=exc)
return ExceptionHandlerMiddleware(
app=request.app, debug=request.app.debug, exception_handlers={}
).default_http_exception_handler(request, exc)
However, the problem is that the exception handler cannot be async. At the same time it seems that I need to await the request body in this handler, so I'm kinda stuck in a pickle.
Drawbacks and Impact
No response
Unresolved questions
No response
Funding
- If you would like to see an issue prioritized, make a pledge towards it!
- We receive the pledge once the issue is completed & verified