Adds Exception Handlers
This commit is contained in:
2
storage_service/utils/exception_handler/__init__.py
Normal file
2
storage_service/utils/exception_handler/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .http_exception_handler import http_exception_handler
|
||||
from .validation_exception_handler import validation_exception_handler
|
||||
@@ -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,
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -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,
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -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
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user