(error: unknown, t: Translator, depth = 0, parentMessage?: string)
| 146 | } |
| 147 | |
| 148 | function formatErrorChain(error: unknown, t: Translator, depth = 0, parentMessage?: string): string { |
| 149 | const json = (value: unknown) => safeJson(value, t("error.page.circular")) |
| 150 | if (!error) return t("error.chain.unknown") |
| 151 | |
| 152 | if (isInitError(error)) { |
| 153 | const message = formatInitError(error, t) |
| 154 | if (depth > 0 && parentMessage === message) return "" |
| 155 | const indent = depth > 0 ? `\n${CHAIN_SEPARATOR}${t("error.chain.causedBy")}\n` : "" |
| 156 | return indent + `${error.name}\n${message}` |
| 157 | } |
| 158 | |
| 159 | if (error instanceof Error) { |
| 160 | const isDuplicate = depth > 0 && parentMessage === error.message |
| 161 | const parts: string[] = [] |
| 162 | const indent = depth > 0 ? `\n${CHAIN_SEPARATOR}${t("error.chain.causedBy")}\n` : "" |
| 163 | |
| 164 | const header = `${error.name}${error.message ? `: ${error.message}` : ""}` |
| 165 | const stack = error.stack?.trim() |
| 166 | |
| 167 | if (stack) { |
| 168 | const startsWithHeader = stack.startsWith(header) |
| 169 | |
| 170 | if (isDuplicate && startsWithHeader) { |
| 171 | const trace = stack.split("\n").slice(1).join("\n").trim() |
| 172 | if (trace) { |
| 173 | parts.push(indent + trace) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (isDuplicate && !startsWithHeader) { |
| 178 | parts.push(indent + stack) |
| 179 | } |
| 180 | |
| 181 | if (!isDuplicate && startsWithHeader) { |
| 182 | parts.push(indent + stack) |
| 183 | } |
| 184 | |
| 185 | if (!isDuplicate && !startsWithHeader) { |
| 186 | parts.push(indent + `${header}\n${stack}`) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (!stack && !isDuplicate) { |
| 191 | parts.push(indent + header) |
| 192 | } |
| 193 | |
| 194 | if (error.cause) { |
| 195 | const causeResult = formatErrorChain(error.cause, t, depth + 1, error.message) |
| 196 | if (causeResult) { |
| 197 | parts.push(causeResult) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return parts.join("\n\n") |
| 202 | } |
| 203 | |
| 204 | if (typeof error === "string") { |
| 205 | if (depth > 0 && parentMessage === error) return "" |
no test coverage detected