From 0acf167d1266fdec17dbaea2a3aee14f4a8d8ce0 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Nakazone Batista Date: Tue, 21 May 2024 19:23:49 -0300 Subject: [PATCH] Adds Exception Handlers --- storage_service/controller/__init__.py | 11 +++++++++- .../utils/exception_handler/__init__.py | 2 ++ .../http_exception_handler.py | 15 ++++++++++++++ .../validation_exception_handler.py | 20 +++++++++++++++++++ .../exceptions/file_not_found_exception.py | 2 +- 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 storage_service/utils/exception_handler/__init__.py create mode 100644 storage_service/utils/exception_handler/http_exception_handler.py create mode 100644 storage_service/utils/exception_handler/validation_exception_handler.py diff --git a/storage_service/controller/__init__.py b/storage_service/controller/__init__.py index 0a27577..67a96b3 100644 --- a/storage_service/controller/__init__.py +++ b/storage_service/controller/__init__.py @@ -1,13 +1,22 @@ +from fastapi.exceptions import RequestValidationError +from starlette.responses import JSONResponse + from storage_service.config.config_allowed_origins import get_allowed_origins from storage_service.controller.health_checker_controller import health_router from storage_service.controller.storage_controller import s3_router -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware +from storage_service.utils.exception_handler import http_exception_handler, validation_exception_handler + app = FastAPI() +app.add_exception_handler(HTTPException, http_exception_handler) +app.add_exception_handler(RequestValidationError, validation_exception_handler) + + app.add_middleware( CORSMiddleware, allow_origins=get_allowed_origins(), diff --git a/storage_service/utils/exception_handler/__init__.py b/storage_service/utils/exception_handler/__init__.py new file mode 100644 index 0000000..27ef06d --- /dev/null +++ b/storage_service/utils/exception_handler/__init__.py @@ -0,0 +1,2 @@ +from .http_exception_handler import http_exception_handler +from .validation_exception_handler import validation_exception_handler diff --git a/storage_service/utils/exception_handler/http_exception_handler.py b/storage_service/utils/exception_handler/http_exception_handler.py new file mode 100644 index 0000000..2df36d1 --- /dev/null +++ b/storage_service/utils/exception_handler/http_exception_handler.py @@ -0,0 +1,15 @@ +from starlette.exceptions import HTTPException +from starlette.requests import Request +from starlette.responses import JSONResponse + + +async def http_exception_handler(request: Request, exc: HTTPException): + return JSONResponse( + status_code=exc.status_code, + content={ + "error": { + "message": exc.detail, + "status_code": exc.status_code, + } + }, + ) \ No newline at end of file diff --git a/storage_service/utils/exception_handler/validation_exception_handler.py b/storage_service/utils/exception_handler/validation_exception_handler.py new file mode 100644 index 0000000..9cc47d7 --- /dev/null +++ b/storage_service/utils/exception_handler/validation_exception_handler.py @@ -0,0 +1,20 @@ +from fastapi.exceptions import RequestValidationError +from starlette import status +from starlette.requests import Request +from starlette.responses import JSONResponse + + +async def validation_exception_handler(request: Request, exc: RequestValidationError): + status_code = status.HTTP_422_UNPROCESSABLE_ENTITY + return JSONResponse( + status_code=status_code, + content={ + "error": { + "details": { + "body": exc.body, + "errors": exc.errors(), + }, + "status_code": status_code, + } + }, + ) \ No newline at end of file diff --git a/storage_service/utils/exceptions/file_not_found_exception.py b/storage_service/utils/exceptions/file_not_found_exception.py index 2e231e1..f61a934 100644 --- a/storage_service/utils/exceptions/file_not_found_exception.py +++ b/storage_service/utils/exceptions/file_not_found_exception.py @@ -4,5 +4,5 @@ from fastapi import HTTPException, status class FileNotFoundException(HTTPException): def __init__(self, message: str): super().__init__( - status.HTTP_400_BAD_REQUEST, detail=message + status.HTTP_404_NOT_FOUND, detail=message )