-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pydantic | ||
from fastapi import ( | ||
Request, | ||
status, | ||
) | ||
from fastapi.responses import JSONResponse | ||
from loguru import logger | ||
|
||
from files_api.monitoring.logger import log_response_info | ||
|
||
|
||
# fastapi docs on middlewares: https://fastapi.tiangolo.com/tutorial/middleware/ | ||
async def handle_broad_exceptions(request: Request, call_next): | ||
"""Handle any exception that goes unhandled by a more specific exception handler.""" | ||
try: | ||
return await call_next(request) | ||
except Exception as err: # pylint: disable=broad-except | ||
logger.exception(err) | ||
response = JSONResponse( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
content={"detail": "Internal server error"}, | ||
) | ||
log_response_info(response) | ||
return response | ||
|
||
|
||
# fastapi docs on error handlers: https://fastapi.tiangolo.com/tutorial/handling-errors/ | ||
# pylint: disable=unused-argument | ||
async def handle_pydantic_validation_errors(request: Request, exc: pydantic.ValidationError) -> JSONResponse: | ||
errors = exc.errors() | ||
logger.exception(exc) | ||
response = JSONResponse( | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
content={ | ||
"detail": [ | ||
{ | ||
"msg": error["msg"], | ||
"input": error["input"], | ||
} | ||
for error in errors | ||
] | ||
}, | ||
) | ||
log_response_info(response) | ||
return response |