(
error: unknown,
format?: { oneline: boolean },
)
| 2 | import { UNICODE_ELLIPSIS, truncateMultilineText } from './formatting.js'; |
| 3 | |
| 4 | export function stringifyError( |
| 5 | error: unknown, |
| 6 | format?: { oneline: boolean }, |
| 7 | ): string { |
| 8 | const truncate = (text: string) => |
| 9 | format?.oneline ? truncateMultilineText(text) : text; |
| 10 | |
| 11 | if (error instanceof ZodError) { |
| 12 | const formattedError = z.prettifyError(error); |
| 13 | if (formattedError.includes('\n')) { |
| 14 | if (format?.oneline) { |
| 15 | return `${error.name} [${UNICODE_ELLIPSIS}]`; |
| 16 | } |
| 17 | return `${error.name}:\n${formattedError}\n`; |
| 18 | } |
| 19 | return `${error.name}: ${formattedError}`; |
| 20 | } |
| 21 | |
| 22 | if (error instanceof Error) { |
| 23 | if (error.name === 'Error' || error.message.startsWith(error.name)) { |
| 24 | return truncate(error.message); |
| 25 | } |
| 26 | return truncate(`${error.name}: ${error.message}`); |
| 27 | } |
| 28 | if (typeof error === 'string') { |
| 29 | return truncate(error); |
| 30 | } |
| 31 | return JSON.stringify(error); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Extends an error with a new message and keeps the original as the cause. |
no test coverage detected