(error: unknown)
| 3 | * It handles circular references and common error properties. |
| 4 | */ |
| 5 | export function stringifyError(error: unknown): string { |
| 6 | const cache = new Set() |
| 7 | const replacer = (_key: string, value: any) => { |
| 8 | if (typeof value === 'object' && value !== null) { |
| 9 | if (cache.has(value)) { |
| 10 | return '[Circular]' |
| 11 | } |
| 12 | cache.add(value) |
| 13 | } |
| 14 | if (value instanceof Error) { |
| 15 | const errorObj: any = {} |
| 16 | Object.getOwnPropertyNames(value).forEach((prop) => { |
| 17 | errorObj[prop] = (value as any)[prop] |
| 18 | }) |
| 19 | return errorObj |
| 20 | } |
| 21 | return value |
| 22 | } |
| 23 | |
| 24 | try { |
| 25 | return JSON.stringify(error, replacer, 2) |
| 26 | } catch { |
| 27 | return String(error) |
| 28 | } finally { |
| 29 | cache.clear() |
| 30 | } |
| 31 | } |
no outgoing calls
no test coverage detected