| 7 | export { serializeValue as serializeError } |
| 8 | |
| 9 | export function processError( |
| 10 | _err: any, |
| 11 | diffOptions?: DiffOptions, |
| 12 | seen: WeakSet<WeakKey> = new WeakSet(), |
| 13 | ): TestError { |
| 14 | if (!_err || typeof _err !== 'object') { |
| 15 | return { message: String(_err) } |
| 16 | } |
| 17 | const err = _err as TestError |
| 18 | |
| 19 | if ( |
| 20 | err.showDiff |
| 21 | || (err.showDiff === undefined |
| 22 | && err.expected !== undefined |
| 23 | && err.actual !== undefined) |
| 24 | ) { |
| 25 | const options = { |
| 26 | ...diffOptions, |
| 27 | ...err.diffOptions as DiffOptions, |
| 28 | } |
| 29 | err.diff = printDiffOrStringify( |
| 30 | err.actual, |
| 31 | err.expected, |
| 32 | options, |
| 33 | err, |
| 34 | ) |
| 35 | |
| 36 | err.expected = prettifyValue(err.expected, options) |
| 37 | err.actual = prettifyValue(err.actual, options) |
| 38 | } |
| 39 | |
| 40 | // some Error implementations may not allow rewriting cause |
| 41 | // in most cases, the assignment will lead to "err.cause = err.cause" |
| 42 | try { |
| 43 | if (!seen.has(err) && typeof err.cause === 'object') { |
| 44 | seen.add(err) |
| 45 | err.cause = processError(err.cause, diffOptions, seen) |
| 46 | } |
| 47 | } |
| 48 | catch {} |
| 49 | |
| 50 | try { |
| 51 | return serializeValue(err) |
| 52 | } |
| 53 | catch (e: any) { |
| 54 | return serializeValue( |
| 55 | new Error( |
| 56 | `Failed to fully serialize error: ${e?.message}\nInner error message: ${err?.message}`, |
| 57 | ), |
| 58 | ) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | function prettifyValue(value: unknown, options: DiffOptions): string | undefined { |
| 63 | if (typeof value !== 'string') { |