不要なエラーハンドラー削除

This commit is contained in:
nik.n 2024-01-30 09:21:04 +09:00
parent ff6dd0b68a
commit 172c6e070b
2 changed files with 2 additions and 34 deletions

View File

@ -10,7 +10,7 @@ from src.controller import (bio, bio_api, healthcheck, login, logout,
from src.core import task
from src.error.exception_handler import http_exception_handler
from src.error.exceptions import UnexpectedException
from src.middleware.middleware import ErrorHandlingMiddleware, SecurityHeadersMiddleware
from src.middleware.middleware import SecurityHeadersMiddleware
app = FastAPI(openapi_url=None)
@ -43,8 +43,7 @@ app.add_exception_handler(status.HTTP_403_FORBIDDEN, http_exception_handler)
# サーバーエラーが発生した場合のハンドラー。HTTPExceptionではハンドリングできないため、個別に設定
app.add_exception_handler(UnexpectedException, http_exception_handler)
# セキュリティヘッダー設定・サーバーエラーや認証失敗はミドルウェアで処理する
app.add_middleware(ErrorHandlingMiddleware)
# セキュリティヘッダー設定はミドルウェアで処理する
app.add_middleware(SecurityHeadersMiddleware)
# サーバー起動時のイベント

View File

@ -14,34 +14,3 @@ class SecurityHeadersMiddleware(BaseHTTPMiddleware):
# Cache-Controlヘッダー追加
response.headers['Cache-Control'] = 'private'
return response
class ErrorHandlingMiddleware(BaseHTTPMiddleware):
# エラーハンドリングをするミドルウェア
# API内で発生したエラーをキャッチして処理を施す
async def dispatch(self, request: Request, call_next) -> Response:
try:
response: Response = await call_next(request)
except TypeError as e:
response = JSONResponse(
{"msg": "TypeError:内容を確認してもう一度データ挿入をしてください。"},
status.HTTP_404_NOT_FOUND,
)
except TimeoutError as e:
response = JSONResponse(
{"msg": "TimeoutError:タイムアウトエラーが発生しました。"},
status.HTTP_408_REQUEST_TIMEOUT,
)
except RuntimeError as e:
response = JSONResponse(
{"msg": "RuntimeError:ランタイムエラーが発生しました。"},
status.HTTP_500_INTERNAL_SERVER_ERROR,
)
except Exception as e:
response = JSONResponse(
{"msg": "Exception:基底クラスエラーが発生しました。"},
status.HTTP_500_INTERNAL_SERVER_ERROR,
)
return response