connectionErrors.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { sanitizeRemoteConnectionLog } from "./connectionLog.js";
  2. import { isPublicZCodeErrorCode, type PublicZCodeErrorCode } from "./errorCodes.js";
  3. export interface RemoteConnectionFailure {
  4. code: PublicZCodeErrorCode;
  5. message: string;
  6. retryable?: boolean;
  7. }
  8. const systemCodes: Record<string, PublicZCodeErrorCode> = {
  9. ECONNREFUSED: "remote-host-unreachable",
  10. EHOSTUNREACH: "remote-host-unreachable",
  11. ENETUNREACH: "remote-host-unreachable",
  12. // Bug 原因:在线断连后的自动恢复经懒连接 probe 抛出 ECONNRESET;码表缺它时被
  13. // probe 的阶段兜底改写成 remote-preflight-failed,UI 误报“设备环境不满足安装要求”。
  14. ECONNRESET: "remote-host-unreachable",
  15. ETIMEDOUT: "remote-connection-timeout",
  16. ENOTFOUND: "remote-host-not-found",
  17. EAI_AGAIN: "remote-host-not-found",
  18. ENOSPC: "remote-space-insufficient",
  19. EACCES: "remote-permission-denied",
  20. EPERM: "remote-permission-denied",
  21. ABORT_ERR: "remote-connection-canceled",
  22. ERR_SSH_HOST_KEY_MISMATCH: "remote-host-key-changed",
  23. };
  24. const phaseCodes: Record<string, PublicZCodeErrorCode> = {
  25. download: "remote-download-failed",
  26. upload: "remote-upload-failed",
  27. preflight: "remote-preflight-failed",
  28. extract: "remote-install-failed",
  29. "verify-archive": "remote-install-failed",
  30. verify: "remote-install-failed",
  31. restart: "remote-install-failed",
  32. install: "remote-install-failed",
  33. };
  34. /** 空 AggregateError 曾生成空响应,被 Main schema 丢弃后 UI 永久等待。 */
  35. export function normalizeRemoteConnectionError(
  36. error: unknown,
  37. phase?: string,
  38. ): RemoteConnectionFailure {
  39. const seen = new Set<unknown>();
  40. function visit(value: unknown, applyPhaseFallback = false): RemoteConnectionFailure {
  41. if (seen.has(value))
  42. return { code: "remote-connection-unknown", message: "Remote connection failed" };
  43. seen.add(value);
  44. const record = value && typeof value === "object" ? (value as Record<string, unknown>) : {};
  45. const message =
  46. typeof record.message === "string"
  47. ? record.message.trim()
  48. : typeof value === "string"
  49. ? value.trim()
  50. : "";
  51. const rawCode = typeof record.code === "string" ? record.code : "";
  52. const explicitCode = isPublicZCodeErrorCode(rawCode) ? rawCode : undefined;
  53. // 适配器已确认的领域原因优先于 cause 的文本猜测;阶段包装仍继续寻找更具体的内层原因。
  54. if (
  55. explicitCode &&
  56. explicitCode !== "remote-connection-unknown" &&
  57. !Object.values(phaseCodes).includes(explicitCode)
  58. ) {
  59. return {
  60. code: explicitCode,
  61. message: sanitizeRemoteConnectionLog(message) || "Remote connection failed",
  62. ...(typeof record.retryable === "boolean" ? { retryable: record.retryable } : {}),
  63. };
  64. }
  65. for (const child of [
  66. ...(Array.isArray(record.errors) ? record.errors : []),
  67. record.cause,
  68. record.error,
  69. ]) {
  70. if (child === undefined) continue;
  71. const nested = visit(child);
  72. if (nested.code !== "remote-connection-unknown")
  73. return { ...nested, message: sanitizeRemoteConnectionLog(message) || nested.message };
  74. }
  75. let code = explicitCode ?? systemCodes[rawCode];
  76. if (!code && record.name === "AbortError") code = "remote-connection-canceled";
  77. if (
  78. !code &&
  79. /all configured authentication methods failed|authentication failed|SSH\s*认证失败/iu.test(
  80. message,
  81. )
  82. )
  83. code = "remote-authentication-failed";
  84. if (
  85. !code &&
  86. /\b(?:ECONNREFUSED|EHOSTUNREACH|ENETUNREACH|ECONNRESET)\b|connection refused|no route to host/i.test(
  87. message,
  88. )
  89. )
  90. code = "remote-host-unreachable";
  91. if (!code && /\bETIMEDOUT\b|timed? out|timeout/i.test(message))
  92. code = "remote-connection-timeout";
  93. if (!code && /\b(?:ENOTFOUND|EAI_AGAIN)\b|could not resolve hostname/i.test(message))
  94. code = "remote-host-not-found";
  95. if (!code && /no space left on device|\bENOSPC\b/i.test(message))
  96. code = "remote-space-insufficient";
  97. if (!code && /permission denied|read-only file system/i.test(message))
  98. code = "remote-permission-denied";
  99. // 仅接受 SSH 明确的密钥校验失败,不从普通 handshake/authentication 失败猜测身份变化。
  100. if (
  101. !code &&
  102. /REMOTE HOST IDENTIFICATION HAS CHANGED|host key verification failed/i.test(message)
  103. )
  104. code = "remote-host-key-changed";
  105. return {
  106. code:
  107. code ??
  108. (message && applyPhaseFallback && phase ? phaseCodes[phase] : undefined) ??
  109. "remote-connection-unknown",
  110. message: sanitizeRemoteConnectionLog(message) || "Remote connection failed",
  111. ...(typeof record.retryable === "boolean" ? { retryable: record.retryable } : {}),
  112. };
  113. }
  114. // 未识别的内层 cause 不是阶段失败事实,不能提前覆盖外层已确认的认证等领域码。
  115. return visit(error, true);
  116. }