errors.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // zcode-site 的错误:错误码到退出码和面向 Agent 的 message / hint 的映射(技术方案 5.8)。
  2. // 和 zcode-site.mjs 一起随插件分发,零依赖。
  3. // 与 api/openapi.yaml 的 x-cli-exit-code 一致。
  4. const EXIT_BY_CODE = {
  5. 3001: 1,
  6. 3002: 3,
  7. 3101: 2,
  8. 3601: 1,
  9. 3602: 1,
  10. 3603: 4,
  11. 3604: 4,
  12. 3605: 3,
  13. 3607: 3,
  14. 3608: 3,
  15. 3609: 1,
  16. 3610: 3,
  17. 3611: 1,
  18. 3612: 3,
  19. 3613: 4,
  20. 3614: 1,
  21. 2001: 4,
  22. 2005: 4,
  23. 2007: 4,
  24. };
  25. // 错误体不带提示文案(技术方案 5.8),面向 Agent 的 message / hint 在这里按 code 生成。
  26. const MESSAGES = {
  27. 3001: ["Invalid request", "Check the command arguments."],
  28. 3002: [
  29. "Too many deployments in the last hour",
  30. "Wait and retry later; the limit is 10 deployments per hour.",
  31. ],
  32. // MaaS JWT 过期时 zcode-server 不按 free 放行,以免付费用户被降级(契约 0.13.0)。
  33. 3101: [
  34. "The coding plan sign-in has expired",
  35. "Ask the user to sign in again, then retry.",
  36. ],
  37. 3601: ["Site not found", "Run `zcode-site list` to see your sites, then pass --site <id>."],
  38. 3602: [
  39. "Deployment not found",
  40. "Run `zcode-site status` to see the latest deployment of this site.",
  41. ],
  42. 3603: [
  43. "The deployment cannot do this in its current state",
  44. "Start a new deployment with `zcode-site deploy`.",
  45. ],
  46. 3604: [
  47. "The package upload is incomplete",
  48. "Run `zcode-site deploy` again to re-upload the package.",
  49. ],
  50. 3605: [
  51. "Account quota exceeded",
  52. "New deployments are paused until the next period. Tell the user that upgrading their plan raises the limits; `zcode-site usage` shows the usage.",
  53. ],
  54. 3607: [
  55. "The package is larger than 200MB",
  56. "Exclude build caches, source maps and dev dependencies from the stage, then deploy again.",
  57. ],
  58. 3608: [
  59. "The current plan cannot hide the ZCode badge",
  60. "Free plans always show the badge; tell the user to upgrade to a paid plan, then run `zcode-site badge hide` again.",
  61. ],
  62. 3609: ["This ZCode version is too old for Sites", "Ask the user to update ZCode."],
  63. 3610: [
  64. "Another deployment is in progress",
  65. "Wait for it to finish (`zcode-site status --site <siteId>`), then retry.",
  66. ],
  67. // 名称和域名前缀在服务端过内容安全机审(契约 0.15.0);只有名称不通过才报错,前缀不通过时服务端换成 site。
  68. 3611: [
  69. "The site name did not pass content review",
  70. "Pick a different display name (--name for a new site, `zcode-site rename` for an existing one) and retry; nothing was created or renamed.",
  71. ],
  72. // 部署上线后过内容审核(契约 0.16.0):人审驳回当前线上版本时站点被下线(offlineReason=review_rejected),用户不能自己改回上线。
  73. 3612: [
  74. "The live version of this site was rejected by content review",
  75. "The site was taken offline and can't be brought back online with this version. Change the content and deploy again; a successful deploy puts the site back online.",
  76. ],
  77. // 访问统计即时查询 RUM 日志(契约 0.19.0):查询失败、超时或排队已满。
  78. 3613: ["Visit analytics are unavailable right now", "Retry in a minute."],
  79. // 改域名前缀时前缀过内容安全机审,不通过时不改(契约 0.19.0,与新建站点时换成 site 不同)。
  80. 3614: [
  81. "The domain prefix did not pass content review",
  82. "Pick a different prefix and retry; the domain was not changed.",
  83. ],
  84. 2001: ["Sites service error", "Retry later."],
  85. 2005: ["Sites storage error", "Retry later."],
  86. 2007: ["Sites upstream error", "Retry later."],
  87. };
  88. export class CliError extends Error {
  89. constructor(code, message, hint, exitCode, data) {
  90. super(message);
  91. this.code = code;
  92. this.hint = hint;
  93. this.exitCode = exitCode;
  94. this.data = data;
  95. }
  96. }
  97. export function apiError(status, body) {
  98. if (status === 401) {
  99. return new CliError(
  100. "UNAUTHENTICATED",
  101. "The Sites credentials were rejected",
  102. "Ask the user to sign in again (or start a new session), then retry once.",
  103. 2,
  104. );
  105. }
  106. const code = body?.code;
  107. const [message, hint] = MESSAGES[code] ?? [`Sites API error (HTTP ${status})`, "Retry later."];
  108. return new CliError(code ?? `HTTP_${status}`, message, hint, EXIT_BY_CODE[code] ?? 4, body?.data);
  109. }
  110. export function localError(code, message, hint) {
  111. return new CliError(code, message, hint, 1);
  112. }