|
|
@@ -7,6 +7,7 @@ import logging
|
|
|
import os
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
import traceback
|
|
|
+import re
|
|
|
from fastapi import FastAPI, Request, HTTPException
|
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
from fastapi.responses import JSONResponse
|
|
|
@@ -40,6 +41,13 @@ if not logger.handlers:
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
+def _clean_message(s: str) -> str:
|
|
|
+ """Normalize exception messages by removing wrapper prefixes like "ValueError,"."""
|
|
|
+ if not s:
|
|
|
+ return s
|
|
|
+ return re.sub(r'^\s*(?:ValueError|Value\s*error)[:,]?\s*', '', str(s), flags=re.I)
|
|
|
+
|
|
|
+
|
|
|
def create_app():
|
|
|
app = FastAPI()
|
|
|
register_mobile_routes(app)
|
|
|
@@ -57,7 +65,8 @@ def create_app():
|
|
|
loc_parts = loc
|
|
|
# join remaining location parts into a dotted path for readability
|
|
|
loc_str = ".".join([str(p) for p in loc_parts if p is not None]) if loc_parts else ""
|
|
|
- errors.append({"field": loc_str, "message": e.get("msg", "")})
|
|
|
+ cleaned_msg = _clean_message(e.get("msg", ""))
|
|
|
+ errors.append({"field": loc_str, "message": cleaned_msg})
|
|
|
|
|
|
# Log validation details for debugging
|
|
|
logger.info("Validation error for %s: %s", request.url.path, errors)
|
|
|
@@ -83,10 +92,12 @@ def create_app():
|
|
|
if isinstance(exc, ValueError):
|
|
|
# Log with stack for debugging
|
|
|
tb = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
|
|
|
- logger.warning("ValueError on %s: %s\n%s", request.url.path, str(exc), tb)
|
|
|
+ raw_msg = str(exc) or ""
|
|
|
+ cleaned_msg = _clean_message(raw_msg)
|
|
|
+ logger.warning("ValueError on %s: %s\n%s", request.url.path, cleaned_msg, tb)
|
|
|
return JSONResponse(
|
|
|
status_code=400,
|
|
|
- content=error_response(msg=str(exc) or "参数错误", code=400),
|
|
|
+ content=error_response(msg=cleaned_msg or "参数错误", code=400),
|
|
|
)
|
|
|
|
|
|
# Generic fallback to avoid leaking internal stack traces
|