|
1 |
| -interface ErrorOptions { |
2 |
| - message?: string |
3 |
| - stackTraceLimit?: number |
4 |
| -} |
5 |
| - |
6 |
| -/** |
7 |
| - * Get original stacktrace without source map support the most performant way. |
8 |
| - * - Create only 1 stack frame. |
9 |
| - * - Rewrite prepareStackTrace to bypass "support-stack-trace" (usually takes ~250ms). |
10 |
| - */ |
11 |
| -export function createSimpleStackTrace(options?: ErrorOptions) { |
12 |
| - const { message = 'error', stackTraceLimit = 1 } = options || {} |
13 |
| - const limit = Error.stackTraceLimit |
14 |
| - const prepareStackTrace = Error.prepareStackTrace |
15 |
| - Error.stackTraceLimit = stackTraceLimit |
16 |
| - Error.prepareStackTrace = e => e.stack |
17 |
| - const err = new Error(message) |
18 |
| - const stackTrace = err.stack || '' |
19 |
| - Error.prepareStackTrace = prepareStackTrace |
20 |
| - Error.stackTraceLimit = limit |
21 |
| - return stackTrace |
| 1 | +import type { DiffOptions } from './diff' |
| 2 | +import { unifiedDiff } from './diff' |
| 3 | +import { format } from './display' |
| 4 | +import { deepClone, getOwnProperties, getType } from './helpers' |
| 5 | +import { stringify } from './stringify' |
| 6 | + |
| 7 | +const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@' |
| 8 | +const IS_COLLECTION_SYMBOL = '@@__IMMUTABLE_ITERABLE__@@' |
| 9 | + |
| 10 | +function isImmutable(v: any) { |
| 11 | + return v && (v[IS_COLLECTION_SYMBOL] || v[IS_RECORD_SYMBOL]) |
| 12 | +} |
| 13 | + |
| 14 | +const OBJECT_PROTO = Object.getPrototypeOf({}) |
| 15 | + |
| 16 | +function getUnserializableMessage(err: unknown) { |
| 17 | + if (err instanceof Error) |
| 18 | + return `<unserializable>: ${err.message}` |
| 19 | + if (typeof err === 'string') |
| 20 | + return `<unserializable>: ${err}` |
| 21 | + return '<unserializable>' |
| 22 | +} |
| 23 | + |
| 24 | +// https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm |
| 25 | +export function serializeError(val: any, seen = new WeakMap()): any { |
| 26 | + if (!val || typeof val === 'string') |
| 27 | + return val |
| 28 | + if (typeof val === 'function') |
| 29 | + return `Function<${val.name || 'anonymous'}>` |
| 30 | + if (typeof val === 'symbol') |
| 31 | + return val.toString() |
| 32 | + if (typeof val !== 'object') |
| 33 | + return val |
| 34 | + // cannot serialize immutables as immutables |
| 35 | + if (isImmutable(val)) |
| 36 | + return serializeError(val.toJSON(), seen) |
| 37 | + if (val instanceof Promise || (val.constructor && val.constructor.prototype === 'AsyncFunction')) |
| 38 | + return 'Promise' |
| 39 | + if (typeof Element !== 'undefined' && val instanceof Element) |
| 40 | + return val.tagName |
| 41 | + if (typeof val.asymmetricMatch === 'function') |
| 42 | + return `${val.toString()} ${format(val.sample)}` |
| 43 | + |
| 44 | + if (seen.has(val)) |
| 45 | + return seen.get(val) |
| 46 | + |
| 47 | + if (Array.isArray(val)) { |
| 48 | + const clone: any[] = new Array(val.length) |
| 49 | + seen.set(val, clone) |
| 50 | + val.forEach((e, i) => { |
| 51 | + try { |
| 52 | + clone[i] = serializeError(e, seen) |
| 53 | + } |
| 54 | + catch (err) { |
| 55 | + clone[i] = getUnserializableMessage(err) |
| 56 | + } |
| 57 | + }) |
| 58 | + return clone |
| 59 | + } |
| 60 | + else { |
| 61 | + // Objects with `Error` constructors appear to cause problems during worker communication |
| 62 | + // using `MessagePort`, so the serialized error object is being recreated as plain object. |
| 63 | + const clone = Object.create(null) |
| 64 | + seen.set(val, clone) |
| 65 | + |
| 66 | + let obj = val |
| 67 | + while (obj && obj !== OBJECT_PROTO) { |
| 68 | + Object.getOwnPropertyNames(obj).forEach((key) => { |
| 69 | + if (key in clone) |
| 70 | + return |
| 71 | + try { |
| 72 | + clone[key] = serializeError(val[key], seen) |
| 73 | + } |
| 74 | + catch (err) { |
| 75 | + // delete in case it has a setter from prototype that might throw |
| 76 | + delete clone[key] |
| 77 | + clone[key] = getUnserializableMessage(err) |
| 78 | + } |
| 79 | + }) |
| 80 | + obj = Object.getPrototypeOf(obj) |
| 81 | + } |
| 82 | + return clone |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function normalizeErrorMessage(message: string) { |
| 87 | + return message.replace(/__vite_ssr_import_\d+__\./g, '') |
| 88 | +} |
| 89 | + |
| 90 | +export function processError(err: any, options: DiffOptions = {}) { |
| 91 | + if (!err || typeof err !== 'object') |
| 92 | + return { message: err } |
| 93 | + // stack is not serialized in worker communication |
| 94 | + // we stringify it first |
| 95 | + if (err.stack) |
| 96 | + err.stackStr = String(err.stack) |
| 97 | + if (err.name) |
| 98 | + err.nameStr = String(err.name) |
| 99 | + |
| 100 | + const clonedActual = deepClone(err.actual, { forceWritable: true }) |
| 101 | + const clonedExpected = deepClone(err.expected, { forceWritable: true }) |
| 102 | + |
| 103 | + const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected) |
| 104 | + |
| 105 | + if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined)) |
| 106 | + err.diff = unifiedDiff(replacedActual, replacedExpected, options) |
| 107 | + |
| 108 | + if (typeof err.expected !== 'string') |
| 109 | + err.expected = stringify(err.expected, 10) |
| 110 | + if (typeof err.actual !== 'string') |
| 111 | + err.actual = stringify(err.actual, 10) |
| 112 | + |
| 113 | + // some Error implementations don't allow rewriting message |
| 114 | + try { |
| 115 | + if (typeof err.message === 'string') |
| 116 | + err.message = normalizeErrorMessage(err.message) |
| 117 | + |
| 118 | + if (typeof err.cause === 'object' && typeof err.cause.message === 'string') |
| 119 | + err.cause.message = normalizeErrorMessage(err.cause.message) |
| 120 | + } |
| 121 | + catch {} |
| 122 | + |
| 123 | + try { |
| 124 | + return serializeError(err) |
| 125 | + } |
| 126 | + catch (e: any) { |
| 127 | + return serializeError(new Error(`Failed to fully serialize error: ${e?.message}\nInner error message: ${err?.message}`)) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function isAsymmetricMatcher(data: any) { |
| 132 | + const type = getType(data) |
| 133 | + return type === 'Object' && typeof data.asymmetricMatch === 'function' |
| 134 | +} |
| 135 | + |
| 136 | +function isReplaceable(obj1: any, obj2: any) { |
| 137 | + const obj1Type = getType(obj1) |
| 138 | + const obj2Type = getType(obj2) |
| 139 | + return obj1Type === obj2Type && obj1Type === 'Object' |
| 140 | +} |
| 141 | + |
| 142 | +export function replaceAsymmetricMatcher(actual: any, expected: any, actualReplaced = new WeakSet(), expectedReplaced = new WeakSet()) { |
| 143 | + if (!isReplaceable(actual, expected)) |
| 144 | + return { replacedActual: actual, replacedExpected: expected } |
| 145 | + if (actualReplaced.has(actual) || expectedReplaced.has(expected)) |
| 146 | + return { replacedActual: actual, replacedExpected: expected } |
| 147 | + actualReplaced.add(actual) |
| 148 | + expectedReplaced.add(expected) |
| 149 | + getOwnProperties(expected).forEach((key) => { |
| 150 | + const expectedValue = expected[key] |
| 151 | + const actualValue = actual[key] |
| 152 | + if (isAsymmetricMatcher(expectedValue)) { |
| 153 | + if (expectedValue.asymmetricMatch(actualValue)) |
| 154 | + actual[key] = expectedValue |
| 155 | + } |
| 156 | + else if (isAsymmetricMatcher(actualValue)) { |
| 157 | + if (actualValue.asymmetricMatch(expectedValue)) |
| 158 | + expected[key] = actualValue |
| 159 | + } |
| 160 | + else if (isReplaceable(actualValue, expectedValue)) { |
| 161 | + const replaced = replaceAsymmetricMatcher( |
| 162 | + actualValue, |
| 163 | + expectedValue, |
| 164 | + actualReplaced, |
| 165 | + expectedReplaced, |
| 166 | + ) |
| 167 | + actual[key] = replaced.replacedActual |
| 168 | + expected[key] = replaced.replacedExpected |
| 169 | + } |
| 170 | + }) |
| 171 | + return { |
| 172 | + replacedActual: actual, |
| 173 | + replacedExpected: expected, |
| 174 | + } |
22 | 175 | }
|
0 commit comments