(error: InitError, t: Translator)
| 55 | } |
| 56 | |
| 57 | function formatInitError(error: InitError, t: Translator): string { |
| 58 | const data = error.data |
| 59 | const json = (value: unknown) => safeJson(value, t("error.page.circular")) |
| 60 | switch (error.name) { |
| 61 | case "MCPFailed": { |
| 62 | const name = typeof data.name === "string" ? data.name : "" |
| 63 | return t("error.chain.mcpFailed", { name }) |
| 64 | } |
| 65 | case "ProviderAuthError": { |
| 66 | const providerID = typeof data.providerID === "string" ? data.providerID : t("common.unknown") |
| 67 | const message = typeof data.message === "string" ? data.message : json(data.message) |
| 68 | return t("error.chain.providerAuthFailed", { provider: providerID, message }) |
| 69 | } |
| 70 | case "APIError": { |
| 71 | const message = typeof data.message === "string" ? data.message : t("error.chain.apiError") |
| 72 | const lines: string[] = [message] |
| 73 | |
| 74 | if (typeof data.statusCode === "number") { |
| 75 | lines.push(t("error.chain.status", { status: data.statusCode })) |
| 76 | } |
| 77 | |
| 78 | if (typeof data.isRetryable === "boolean") { |
| 79 | lines.push(t("error.chain.retryable", { retryable: data.isRetryable })) |
| 80 | } |
| 81 | |
| 82 | if (typeof data.responseBody === "string" && data.responseBody) { |
| 83 | lines.push(t("error.chain.responseBody", { body: data.responseBody })) |
| 84 | } |
| 85 | |
| 86 | return lines.join("\n") |
| 87 | } |
| 88 | case "ProviderModelNotFoundError": { |
| 89 | const { providerID, modelID, suggestions } = data as { |
| 90 | providerID: string |
| 91 | modelID: string |
| 92 | suggestions?: string[] |
| 93 | } |
| 94 | |
| 95 | const suggestionsLine = |
| 96 | Array.isArray(suggestions) && suggestions.length |
| 97 | ? [t("error.chain.didYouMean", { suggestions: suggestions.join(", ") })] |
| 98 | : [] |
| 99 | |
| 100 | return [ |
| 101 | t("error.chain.modelNotFound", { provider: providerID, model: modelID }), |
| 102 | ...suggestionsLine, |
| 103 | t("error.chain.checkConfig"), |
| 104 | ].join("\n") |
| 105 | } |
| 106 | case "ProviderInitError": { |
| 107 | const providerID = typeof data.providerID === "string" ? data.providerID : t("common.unknown") |
| 108 | return t("error.chain.providerInitFailed", { provider: providerID }) |
| 109 | } |
| 110 | case "ConfigJsonError": { |
| 111 | const path = typeof data.path === "string" ? data.path : json(data.path) |
| 112 | const message = typeof data.message === "string" ? data.message : "" |
| 113 | if (message) return t("error.chain.configJsonInvalidWithMessage", { path, message }) |
| 114 | return t("error.chain.configJsonInvalid", { path }) |
no test coverage detected