提交
This commit is contained in:
1 parent
2020f371f4
commit
f1f5d24eff
31 files changed
+13600
-13813
No files matched your search
@@ -36,6 +36,23 @@ def success_response(data: Any = None, message: str = "success") -> dict:
|
||||
}
|
||||
|
||||
|
||||
def format_api_error_message(exc: Exception, *, fallback: str = "操作失败") -> str:
|
||||
"""将 HTTP/SQLAlchemy 等异常转为可展示的错误文案(避免 str([]) 空列表)。"""
|
||||
if isinstance(exc, PermissionError):
|
||||
return str(exc) or fallback
|
||||
status_code = getattr(exc, "status_code", None)
|
||||
if status_code is not None:
|
||||
detail = getattr(exc, "detail", None)
|
||||
if isinstance(detail, str) and detail.strip():
|
||||
return detail
|
||||
if detail not in (None, "", []):
|
||||
return str(detail)
|
||||
msg = str(exc).strip()
|
||||
if msg and msg != "[]":
|
||||
return msg
|
||||
return fallback
|
||||
|
||||
|
||||
def error_response(code: int = 400, message: str = "error", data: Any = None) -> dict:
|
||||
"""错误响应"""
|
||||
from datetime import datetime
|
||||
|
||||
Reference in new issue
Block a user