feat: GPT Image 模型名映射及全局报错文案优化
- GPT Image 模型下拉选项映射为实际 API 参数(次卡→gpt-image-2-c,按量→gpt-image-2) - 全局:含 "high load" 关键词的错误统一展示为"模型过载,请稍后重试!" - GPT 独立:500 错误展示为"触发内容风控,或服务器繁忙!" Co-Authored-By: Claude Opus 4.7 <[email protected]>
This commit is contained in:
+13
-4
@@ -26,6 +26,11 @@ HTTP_ERROR_MESSAGES = {
|
||||
504: "网关超时。请稍后重试。",
|
||||
}
|
||||
|
||||
# 错误内容关键词 → 用户友好文案(优先于状态码匹配)
|
||||
ERROR_CONTENT_MESSAGES = {
|
||||
"The current model has a high load": "模型过载,请稍后重试!",
|
||||
}
|
||||
|
||||
# 可退避重试的状态码
|
||||
RETRYABLE_STATUS_CODES = {429, 502, 503, 504}
|
||||
|
||||
@@ -37,7 +42,11 @@ DEFAULT_BACKOFF_FACTOR = 2.0 # 指数退避因子
|
||||
|
||||
|
||||
def get_friendly_message(status_code: int, raw_message: str = "") -> str:
|
||||
"""根据状态码返回友好文案,未匹配则返回原始信息"""
|
||||
"""根据状态码/错误内容返回友好文案,未匹配则返回原始信息"""
|
||||
if raw_message:
|
||||
for keyword, friendly_msg in ERROR_CONTENT_MESSAGES.items():
|
||||
if keyword in raw_message:
|
||||
return friendly_msg
|
||||
friendly = HTTP_ERROR_MESSAGES.get(status_code)
|
||||
if friendly:
|
||||
return friendly
|
||||
@@ -119,7 +128,7 @@ async def async_request_with_retry(
|
||||
break
|
||||
|
||||
if last_status and last_status in HTTP_ERROR_MESSAGES:
|
||||
raise_for_status(last_status, prefix=prefix)
|
||||
raise_for_status(last_status, raw_message=last_message, prefix=prefix)
|
||||
|
||||
raw_msg = last_message[:200] if last_message else ""
|
||||
raise RuntimeError(f"{prefix}请求失败 ({last_status}): {raw_msg}")
|
||||
friendly = get_friendly_message(last_status or 0, last_message)
|
||||
raise RuntimeError(f"{prefix}{friendly}")
|
||||
|
||||
Reference in New Issue
Block a user