|
1 |
| -import { Error, safePush, safeReplace } from '../../../utils/globals'; |
| 1 | +import { Error, safeErrorToString, safePush, safeReplace, safeToString, String } from '../../../utils/globals'; |
2 | 2 | import { stringify, possiblyAsyncStringify } from '../../../utils/stringify';
|
3 | 3 | import { VerbosityLevel } from '../configuration/VerbosityLevel';
|
4 | 4 | import { ExecutionStatus } from '../reporter/ExecutionStatus';
|
@@ -79,10 +79,48 @@ function preFormatTooManySkipped<Ts>(out: RunDetailsFailureTooManySkips<Ts>, str
|
79 | 79 | return { message, details, hints };
|
80 | 80 | }
|
81 | 81 |
|
| 82 | +/** @internal */ |
| 83 | +function prettyError(errorInstance: unknown) { |
| 84 | + // Print the Error message and its associated stacktrace |
| 85 | + if (errorInstance instanceof Error && errorInstance.stack !== undefined) { |
| 86 | + return errorInstance.stack; // stack includes the message |
| 87 | + } |
| 88 | + |
| 89 | + // First fallback: String(.) |
| 90 | + try { |
| 91 | + return String(errorInstance); |
| 92 | + } catch (_err) { |
| 93 | + // no-op |
| 94 | + } |
| 95 | + |
| 96 | + // Second fallback: Error::toString() |
| 97 | + if (errorInstance instanceof Error) { |
| 98 | + try { |
| 99 | + return safeErrorToString(errorInstance); |
| 100 | + } catch (_err) { |
| 101 | + // no-op |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Third fallback: Object::toString() |
| 106 | + if (errorInstance !== null && typeof errorInstance === 'object') { |
| 107 | + try { |
| 108 | + return safeToString(errorInstance); |
| 109 | + } catch (_err) { |
| 110 | + // no-op |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Final fallback: Hardcoded string |
| 115 | + return 'Failed to serialize errorInstance'; |
| 116 | +} |
| 117 | + |
82 | 118 | /** @internal */
|
83 | 119 | function preFormatFailure<Ts>(out: RunDetailsFailureProperty<Ts>, stringifyOne: (value: Ts) => string) {
|
84 | 120 | const noErrorInMessage = out.runConfiguration.errorWithCause;
|
85 |
| - const messageErrorPart = noErrorInMessage ? '' : `\nGot ${safeReplace(out.error, /^Error: /, 'error: ')}`; |
| 121 | + const messageErrorPart = noErrorInMessage |
| 122 | + ? '' |
| 123 | + : `\nGot ${safeReplace(prettyError(out.errorInstance), /^Error: /, 'error: ')}`; |
86 | 124 | const message = `Property failed after ${out.numRuns} tests\n{ seed: ${out.seed}, path: "${
|
87 | 125 | out.counterexamplePath
|
88 | 126 | }", endOnFailure: true }\nCounterexample: ${stringifyOne(out.counterexample)}\nShrunk ${
|
|
0 commit comments