Доработала compose.yaml и main.py для успешного прохождения всех тестов
This commit is contained in:
+7
-2
@@ -201,11 +201,14 @@ def _unauthorized_challenge(path: str) -> str:
|
||||
|
||||
|
||||
def _error_response(status_code: int, message: str, request_id: str,
|
||||
path: str = "") -> JSONResponse:
|
||||
path: str = "", extra_headers: dict | None = None) -> JSONResponse:
|
||||
code = _ERROR_CODES.get(status_code, "internal_error")
|
||||
headers = {}
|
||||
if status_code == 401:
|
||||
headers["WWW-Authenticate"] = _unauthorized_challenge(path)
|
||||
# lab7: пробрасываем заголовки из HTTPException (например, Retry-After у 429).
|
||||
if extra_headers:
|
||||
headers.update(extra_headers)
|
||||
headers["X-Request-Id"] = request_id
|
||||
headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return JSONResponse(
|
||||
@@ -219,7 +222,9 @@ def _error_response(status_code: int, message: str, request_id: str,
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
request_id = getattr(request.state, "request_id", uuid.uuid4().hex)
|
||||
message = exc.detail if isinstance(exc.detail, str) else "Ошибка"
|
||||
return _error_response(exc.status_code, message, request_id, request.url.path)
|
||||
# lab7: сохраняем заголовки, установленные в HTTPException (Retry-After и т.п.).
|
||||
return _error_response(exc.status_code, message, request_id, request.url.path,
|
||||
extra_headers=dict(exc.headers or {}))
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
|
||||
Reference in New Issue
Block a user