errors.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """面向用户的错误类型,以及把 HTTP 状态翻译成人话。"""
  2. class WpError(Exception):
  3. """面向用户的错误:main() 捕获后只打印 message,不打印堆栈。"""
  4. class ApiError(WpError):
  5. def __init__(self, status, message, url=None, body=None):
  6. self.status = status
  7. self.url = url
  8. self.body = body
  9. super().__init__(message)
  10. def explain_api_error(exc, what):
  11. """把 HTTP 状态翻译成对操作者有意义的话(见 references/api.md 的错误约定)。"""
  12. if exc.status == 401:
  13. return WpError(
  14. f"{what}:401 凭据失效或已被撤销。\n"
  15. " · 用户 token 失效 → 重新登录:! python3 scripts/wp_login.py\n"
  16. " · 模型 token 失效或被撤销 → 重跑:python3 wp.py ensure-model\n"
  17. " 不要自动重试。"
  18. )
  19. if exc.status == 403:
  20. return WpError(f"{what}:403 无权限(不是 channel 的 owner/协作者,或不是模型 owner 本人)。")
  21. if exc.status == 404:
  22. return WpError(
  23. f"{what}:404。若这是较新的端点,可能是当前站点跑的是稳定版代码、端点尚未上线;\n"
  24. " 可切到最新版试试:python3 wp.py endpoint next\n"
  25. " 否则才是资源真的不存在。"
  26. )
  27. if exc.status == 409:
  28. return WpError(f"{what}:409 同名记录已存在。")
  29. if exc.status == 422:
  30. return WpError(f"{what}:422 参数校验失败——{exc}")
  31. return WpError(f"{what}:HTTP {exc.status} {exc}")