@@ -84,25 +84,21 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
84
84
85
85
@app .exception_handler (StarletteHTTPException )
86
86
async def http_exception_handler (request , exc ):
87
- await log_exception (request , exc , body = 'StarletteHTTPException handler cannot read body atm. Waiting for FastAPI upgrade.' , details = exc .detail )
87
+ body = await request .body ()
88
+ await log_exception (request , exc , body = body , details = exc .detail )
88
89
return ORJSONResponse (
89
90
status_code = exc .status_code ,
90
91
content = jsonable_encoder ({'success' : False , 'err' : exc .detail }),
91
92
)
92
93
93
94
async def catch_exceptions_middleware (request : Request , call_next ):
94
95
#pylint: disable=broad-except
96
+ body = None
95
97
try :
98
+ body = await request .body ()
96
99
return await call_next (request )
97
100
except Exception as exc :
98
- # body = await request.body() # This blocks the application. Unclear atm how to handle it properly
99
- # seems like a bug: https://github.com/tiangolo/fastapi/issues/394
100
- # Although the issue is closed the "solution" still behaves with same failure
101
- # Actually Starlette, the underlying library to FastAPI has already introduced this functionality:
102
- # https://github.com/encode/starlette/pull/1692
103
- # However FastAPI does not support the new Starlette 0.31.1
104
- # The PR relevant here is: https://github.com/tiangolo/fastapi/pull/9939
105
- await log_exception (request , exc , body = 'Middleware cannot read body atm. Waiting for FastAPI upgrade' )
101
+ await log_exception (request , exc , body = body )
106
102
return ORJSONResponse (
107
103
content = {
108
104
'success' : False ,
0 commit comments