forked from mtusi2026/mtusi2026lab2
Исправления после проверки: кэш, cursor, обработка ошибок
This commit is contained in:
+89
-1
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
|
||||
@@ -55,6 +55,94 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_exception_handler(request: Request, exc: HTTPException):
|
||||
rid = getattr(request.state, "request_id", uuid.uuid4().hex)
|
||||
|
||||
headers = {"X-Request-Id": rid}
|
||||
if exc.headers:
|
||||
headers.update(exc.headers)
|
||||
|
||||
code_map = {
|
||||
400: "invalid_request",
|
||||
401: "unauthorized",
|
||||
404: "not_found",
|
||||
409: "conflict",
|
||||
413: "payload_too_large",
|
||||
415: "unsupported_media_type",
|
||||
429: "rate_limited",
|
||||
503: "unavailable",
|
||||
}
|
||||
code = code_map.get(exc.status_code, "internal_error")
|
||||
|
||||
message = exc.detail if isinstance(exc.detail, str) else "error"
|
||||
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={
|
||||
"error": {
|
||||
"code": code,
|
||||
"message": message,
|
||||
"request_id": rid,
|
||||
}
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
# PostgreSQL: ошибки соединения → 503
|
||||
try:
|
||||
from asyncpg.exceptions import (
|
||||
PostgresConnectionError,
|
||||
TooManyConnectionsError,
|
||||
CannotConnectNowError,
|
||||
ConnectionDoesNotExistError,
|
||||
)
|
||||
|
||||
@app.exception_handler(PostgresConnectionError)
|
||||
@app.exception_handler(TooManyConnectionsError)
|
||||
@app.exception_handler(CannotConnectNowError)
|
||||
@app.exception_handler(ConnectionDoesNotExistError)
|
||||
async def postgres_unavailable_handler(request: Request, exc: Exception):
|
||||
rid = getattr(request.state, "request_id", uuid.uuid4().hex)
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unavailable",
|
||||
"message": "database unavailable",
|
||||
"request_id": rid,
|
||||
}
|
||||
},
|
||||
headers={"X-Request-Id": rid},
|
||||
)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
# Redis: ошибки соединения → 503
|
||||
try:
|
||||
import redis.exceptions as redis_exceptions
|
||||
|
||||
@app.exception_handler(redis_exceptions.ConnectionError)
|
||||
@app.exception_handler(redis_exceptions.TimeoutError)
|
||||
async def redis_unavailable_handler(request: Request, exc: Exception):
|
||||
rid = getattr(request.state, "request_id", uuid.uuid4().hex)
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unavailable",
|
||||
"message": "cache unavailable",
|
||||
"request_id": rid,
|
||||
}
|
||||
},
|
||||
headers={"X-Request-Id": rid},
|
||||
)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def generic_exception_handler(request: Request, exc: Exception):
|
||||
rid = getattr(request.state, "request_id", uuid.uuid4().hex)
|
||||
|
||||
Reference in New Issue
Block a user