feat: complete T-530 cmhub URL normalization

This commit is contained in:
chengma
2026-07-06 10:31:36 +08:00
parent 42dd8a993f
commit b99019e954
14 changed files with 142 additions and 31 deletions
+12 -1
View File
@@ -771,7 +771,7 @@ def _cmhub_error_from_response(data, response, api_key):
error = data.get("error") if isinstance(data, dict) else None
if not isinstance(error, dict):
error = {}
code = str(error.get("code") or _cmhub_code_for_status(status))
code = _normalize_cmhub_error_code(error.get("code") or _cmhub_code_for_status(status), status)
raw_message = (error.get("message") or data.get("message")) if isinstance(data, dict) else ""
if not raw_message:
raw_message = getattr(response, "text", "")[:500]
@@ -786,12 +786,20 @@ def _cmhub_error_from_response(data, response, api_key):
)
def _normalize_cmhub_error_code(code, status=None):
normalized = str(code or "unknown").strip().lower().replace("-", "_")
if status == 404 or normalized in {"notfound", "not_found"}:
return "not_found"
return normalized or "unknown"
def _cmhub_code_for_status(status):
return {
400: "bad_request",
401: "unauthorized",
402: "insufficient_points",
403: "account_disabled",
404: "not_found",
429: "rate_limited",
502: "upstream_error",
}.get(status, "unknown")
@@ -812,8 +820,11 @@ def _cmhub_user_message(code, message):
"content_blocked": "cmhub 内容安全策略拒绝本次生成",
"upstream_error": "cmhub 上游生成失败,请稍后重试",
"rate_limited": "cmhub 请求过于频繁,请稍后重试",
"not_found": "cmhub 接口不存在,请检查 Base URL 或该实例是否已部署 /api/v1/models",
}
default = defaults.get(str(code))
if str(code) == "not_found":
return default
if default and message and str(message) not in default:
return "%s:%s" % (default, message)
return default or str(message or code)