Coverage for app/core/middleware/exception_handling_middleware.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
1from fastapi import Request, Response
2from fastapi.responses import JSONResponse
3from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
5from app.core.base import BaseError
6from app.ddd.infrastructure.util import recursive_to_camel
9class ErrorHandlingMiddleware(BaseHTTPMiddleware):
10 async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
11 try:
12 response: Response = await call_next(request)
13 except BaseError as e:
14 print(e)
15 print("on error")
16 # TODO(nonomura): エラー設計後
17 # response = JSONResponse(
18 # status_code=500,
19 # content={"detail": "An internal server error occurred."},
20 # )
21 # raise
23 # response = e.detail().model_dump(by_alias=True)
24 # print(response)
25 response = JSONResponse(
26 status_code=e.status_code(),
27 # content={"detail": e.detail()},
28 # content=e.detail(),
29 content=recursive_to_camel(e.detail())
30 )
32 except Exception:
33 raise # ハンドリングしない場合
34 # print(e)
35 # print("on error")
36 # # TODO(nonomura): エラー設計後
37 # response = JSONResponse(
38 # status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
39 # content={"detail": "An internal server error occurred."},
40 # )
41 print(response)
42 return response