first commit
This commit is contained in:
9
service_app/handlers_exception/__init__.py
Normal file
9
service_app/handlers_exception/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from .api_exception_handlers.http_exception_handler import (
|
||||
exception_handler_http,
|
||||
exception_handler_exception,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"exception_handler_http",
|
||||
"exception_handler_exception",
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
from json import loads
|
||||
from fastapi import status
|
||||
|
||||
from fastapi.requests import Request
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
def exception_handler_http(request: Request, exc: HTTPException):
|
||||
print('headers', request.headers)
|
||||
detail = loads(exc.detail)
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={
|
||||
"Data": detail.get('data', {}),
|
||||
"Error": detail.get('error_case', 'UNKNOWN'),
|
||||
"Message": detail.get('message', 'An error occurred while processing the request')
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def exception_handler_exception(request: Request, exc: Exception):
|
||||
print('headers', request.headers)
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_417_EXPECTATION_FAILED, content={"message": exc.__str__()}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user