Skip to content

Commit

Permalink
Create errors.py
Browse files Browse the repository at this point in the history
  • Loading branch information
DoganK01 authored Nov 23, 2024
1 parent e177576 commit 9362926
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/files_api/errors.py
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

0 comments on commit 9362926

Please sign in to comment.