fix: 素材 API 错误信息中文映射(同 Seedance 模式)
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m27s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m27s
AssetsAPIError 加 user_message,按 code/关键词映射中文提示,用户不再看到英文错误 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
0a1a3a266c
commit
969283690f
@ -2726,10 +2726,8 @@ def _assets_api_call(func, *args, **kwargs):
|
|||||||
return result, None
|
return result, None
|
||||||
except AssetsAPIError as e:
|
except AssetsAPIError as e:
|
||||||
logger.warning('Assets API error: %s', e)
|
logger.warning('Assets API error: %s', e)
|
||||||
# Surface readable message for known parameter errors
|
|
||||||
msg = e.api_message if hasattr(e, 'api_message') else str(e)
|
|
||||||
return None, Response(
|
return None, Response(
|
||||||
{'error': 'assets_api_error', 'message': msg},
|
{'error': 'assets_api_error', 'message': e.user_message},
|
||||||
status=status.HTTP_502_BAD_GATEWAY,
|
status=status.HTTP_502_BAD_GATEWAY,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@ -21,12 +21,41 @@ HOST = 'open.volcengineapi.com'
|
|||||||
PROJECT_NAME = 'int_dev_Airlabs'
|
PROJECT_NAME = 'int_dev_Airlabs'
|
||||||
|
|
||||||
|
|
||||||
|
_ASSETS_ERROR_MESSAGES = {
|
||||||
|
'ConfigError': '素材服务未配置,请联系管理员',
|
||||||
|
'RequestError': '素材服务暂时不可用,请稍后重试',
|
||||||
|
'InvalidParameter': '素材参数无效,请检查输入',
|
||||||
|
'NotFound': '素材不存在或已被删除',
|
||||||
|
'NotExist': '素材不存在或已被删除',
|
||||||
|
'InternalError': '素材服务异常,请稍后重试',
|
||||||
|
'Forbidden': '没有权限操作该素材',
|
||||||
|
'RateLimitExceeded': '操作过于频繁,请稍后重试',
|
||||||
|
}
|
||||||
|
|
||||||
|
_ASSETS_MESSAGE_KEYWORDS = {
|
||||||
|
'dimension': '图片尺寸不符合要求(宽高需在 300~6000 像素之间)',
|
||||||
|
'size': '文件大小超出限制',
|
||||||
|
'format': '不支持的文件格式',
|
||||||
|
'not found': '素材不存在或已被删除',
|
||||||
|
'permission': '没有权限操作该素材',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class AssetsAPIError(Exception):
|
class AssetsAPIError(Exception):
|
||||||
"""Raised when the Assets API returns an error."""
|
"""Raised when the Assets API returns an error."""
|
||||||
def __init__(self, code, message, status_code=400):
|
def __init__(self, code, message, status_code=400):
|
||||||
self.code = code
|
self.code = code
|
||||||
self.api_message = message
|
self.api_message = message
|
||||||
self.status_code = status_code
|
self.status_code = status_code
|
||||||
|
# 中文友好提示
|
||||||
|
friendly = _ASSETS_ERROR_MESSAGES.get(code)
|
||||||
|
if not friendly:
|
||||||
|
msg_lower = (message or '').lower()
|
||||||
|
for keyword, hint in _ASSETS_MESSAGE_KEYWORDS.items():
|
||||||
|
if keyword in msg_lower:
|
||||||
|
friendly = hint
|
||||||
|
break
|
||||||
|
self.user_message = friendly or '素材操作失败,请稍后重试'
|
||||||
super().__init__(f'[{code}] {message}')
|
super().__init__(f'[{code}] {message}')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user