This commit is contained in:
2026-08-10 18:58:31 +08:00
parent 80d0fd26f1
commit decabb5c42
2 changed files with 10 additions and 2 deletions
@@ -3,7 +3,7 @@ import json
import logging import logging
from cryptography.hazmat.primitives.ciphers.aead import AESGCM from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from fastapi import Request, Response from fastapi import HTTPException, Request, Response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from app.config import settings from app.config import settings
@@ -98,6 +98,14 @@ class RequestEncryptMiddleware(BaseHTTPMiddleware):
status_code=response.status_code, status_code=response.status_code,
headers={"X-Encrypted": "true", "Content-Type": "application/json"}, headers={"X-Encrypted": "true", "Content-Type": "application/json"},
) )
except HTTPException as http_exc:
# BaseHTTPMiddleware 中 call_next 抛出的 HTTPException 会直接传播,
# 这里捕获后返回对应状态码的响应,避免被外层 except Exception 吞掉
return Response(
content=json.dumps({"detail": http_exc.detail}, ensure_ascii=False),
status_code=http_exc.status_code,
headers={"Content-Type": "application/json"},
)
except Exception: except Exception:
logger.exception("Request decryption failed") logger.exception("Request decryption failed")
return Response( return Response(
+1 -1
View File
@@ -93,7 +93,7 @@ async def create_invoice(
for o in occupied for o in occupied
) )
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_409_CONFLICT,
detail=details, detail=details,
) )