错误处理

错误响应格式

Token8 的错误响应格式与 OpenAI API 对齐,方便 SDK 统一处理:

{
  "error": {
    "message": "Validation failed",
    "type": "invalid_request_error",
    "code": "validation_error",
    "details": [
      { "path": "prompt", "message": "Required" }
    ]
  }
}

错误类型

type说明
invalid_request_error请求参数有误
auth_errorAPI Key 无效、已禁用或过期
billing_error余额不足或定价未配置
routing_error无可用供应商
not_found资源不存在
server_error配置错误(如 JWT_SECRET 缺失)
api_errorToken8 内部未知错误

HTTP 状态码

状态码type处理建议
400invalid_request_error检查请求参数
401auth_error检查 API Key 是否正确
402billing_error余额不足,充值后重试
403auth_errorAPI Key 已禁用或过期
404not_found资源不存在
429billing_errorToken 额度耗尽
500server_error / api_error联系支持或稍后重试
503routing_error无可用供应商,稍后重试

任务级错误

任务执行过程中的错误体现在 GET /v1/tasks/:task_id 的响应中:

{
  "task_id": "task_abc123def456",
  "status": "failed",
  "model": "gpt-image-2",
  "fail_reason": "content_policy_violation",
  "message": "提示词或参考图未通过内容安全检测,请修改后重试",
  "total_cost": 0,
  "attempt_count": 1
}

常见任务失败原因

fail_reason说明是否自动重试
upstream_500 / upstream_503上游商家返回服务错误
timeout_120s / timeout_240s上游提交超时
poll_timeout异步任务轮询超时
content_policy_violation提示词或参考图未通过内容安全检测
no_more_providers已无可切换商家
insufficient_user_quota / insufficient_token_quota重试预扣费时额度不足

开启「图片/视频生成重试」后,可恢复的上游失败会自动切换商家,直到达到最大尝试次数。失败 attempt 的费用会自动退回。 内容安全失败会直接终止任务,不会因为无可切换商家而包装成 no_more_providers