| 1 |
/** |
| 2 |
* This file uses the iternal V8 Stack Trace API (https://v8.dev/docs/stack-trace-api) |
| 3 |
* to provide utilities to rewrite the stack trace. |
| 4 |
* When this API is not present, all the functions in this file become noops. |
| 5 |
* |
| 6 |
* beginHiddenCallStack(fn) and endHiddenCallStack(fn) wrap their parameter to |
| 7 |
* mark an hidden portion of the stack trace. The function passed to |
| 8 |
* beginHiddenCallStack is the first hidden function, while the function passed |
| 9 |
* to endHiddenCallStack is the first shown function. |
| 10 |
* |
| 11 |
* When an error is thrown _outside_ of the hidden zone, everything between |
| 12 |
* beginHiddenCallStack and endHiddenCallStack will not be shown. |
| 13 |
* If an error is thrown _inside_ the hidden zone, then the whole stack trace |
| 14 |
* will be visible: this is to avoid hiding real bugs. |
| 15 |
* However, if an error inside the hidden zone is expected, it can be marked |
| 16 |
* with the expectedError(error) function to keep the hidden frames hidden. |
| 17 |
* |
| 18 |
* Consider this call stack (the outer function is the bottom one): |
| 19 |
* |
| 20 |
* 1. a() |
| 21 |
* 2. endHiddenCallStack(b)() |
| 22 |
* 3. c() |
| 23 |
* 4. beginHiddenCallStack(d)() |
| 24 |
* 5. e() |
| 25 |
* 6. f() |
| 26 |
* |
| 27 |
* - If a() throws an error, then its shown call stack will be "a, b, e, f" |
| 28 |
* - If b() throws an error, then its shown call stack will be "b, e, f" |
| 29 |
* - If c() throws an expected error, then its shown call stack will be "e, f" |
| 30 |
* - If c() throws an unexpected error, then its shown call stack will be "c, d, e, f" |
| 31 |
* - If d() throws an expected error, then its shown call stack will be "e, f" |
| 32 |
* - If d() throws an unexpected error, then its shown call stack will be "d, e, f" |
| 33 |
* - If e() throws an error, then its shown call stack will be "e, f" |
| 34 |
* |
| 35 |
* Additionally, an error can inject additional "virtual" stack frames using the |
| 36 |
* injcectVirtualStackFrame(error, filename) function: those are injected as a |
| 37 |
* replacement of the hidden frames. |
| 38 |
* In the example above, if we called injcectVirtualStackFrame(err, "h") and |
| 39 |
* injcectVirtualStackFrame(err, "i") on the expected error thrown by c(), its |
| 40 |
* shown call stack would have been "h, i, e, f". |
| 41 |
* This can be useful, for example, to report config validation errors as if they |
| 42 |
* were directly thrown in the config file. |
| 43 |
*/ |
| 44 |
|
| 45 |
const ErrorToString = Function.call.bind(Error.prototype.toString); |
| 46 |
|
| 47 |
const SUPPORTED = !!Error.captureStackTrace; |
| 48 |
|
| 49 |
const START_HIDNG = "startHiding - secret - don't use this - v1"; |
| 50 |
const STOP_HIDNG = "stopHiding - secret - don't use this - v1"; |
| 51 |
|
| 52 |
type CallSite = Parameters<typeof Error.prepareStackTrace>[1][number]; |
| 53 |
|
| 54 |
const expectedErrors = new WeakSet<Error>(); |
| 55 |
const virtualFrames = new WeakMap<Error, CallSite[]>(); |
| 56 |
|
| 57 |
function CallSite(filename: string): CallSite { |
| 58 |
// We need to use a prototype otherwise it breaks source-map-support's internals |
| 59 |
return Object.create({ |
| 60 |
isNative: () => false, |
| 61 |
isConstructor: () => false, |
| 62 |
isToplevel: () => true, |
| 63 |
getFileName: () => filename, |
| 64 |
getLineNumber: () => undefined, |
| 65 |
getColumnNumber: () => undefined, |
| 66 |
getFunctionName: () => undefined, |
| 67 |
getMethodName: () => undefined, |
| 68 |
getTypeName: () => undefined, |
| 69 |
toString: () => filename, |
| 70 |
} as CallSite); |
| 71 |
} |
| 72 |
|
| 73 |
export function injcectVirtualStackFrame(error: Error, filename: string) { |
| 74 |
if (!SUPPORTED) return; |
| 75 |
|
| 76 |
let frames = virtualFrames.get(error); |
| 77 |
if (!frames) virtualFrames.set(error, (frames = [])); |
| 78 |
frames.push(CallSite(filename)); |
| 79 |
|
| 80 |
return error; |
| 81 |
} |
| 82 |
|
| 83 |
export function expectedError(error: Error) { |
| 84 |
if (!SUPPORTED) return; |
| 85 |
expectedErrors.add(error); |
| 86 |
return error; |
| 87 |
} |
| 88 |
|
| 89 |
export function beginHiddenCallStack<A extends unknown[], R>( |
| 90 |
fn: (...args: A) => R, |
| 91 |
) { |
| 92 |
if (!SUPPORTED) return fn; |
| 93 |
|
| 94 |
return Object.defineProperty( |
| 95 |
function (...args: A) { |
| 96 |
setupPrepareStackTrace(); |
| 97 |
return fn(...args); |
| 98 |
}, |
| 99 |
"name", |
| 100 |
{ value: STOP_HIDNG }, |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
export function endHiddenCallStack<A extends unknown[], R>( |
| 105 |
fn: (...args: A) => R, |
| 106 |
) { |
| 107 |
if (!SUPPORTED) return fn; |
| 108 |
|
| 109 |
return Object.defineProperty( |
| 110 |
function (...args: A) { |
| 111 |
return fn(...args); |
| 112 |
}, |
| 113 |
"name", |
| 114 |
{ value: START_HIDNG }, |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
function setupPrepareStackTrace() { |
| 119 |
// @ts-expect-error This function is a singleton |
| 120 |
// eslint-disable-next-line no-func-assign |
| 121 |
setupPrepareStackTrace = () => {}; |
| 122 |
|
| 123 |
const { prepareStackTrace = defaultPrepareStackTrace } = Error; |
| 124 |
|
| 125 |
// We add some extra frames to Error.stackTraceLimit, so that we can |
| 126 |
// always show some useful frames even after deleting ours. |
| 127 |
// STACK_TRACE_LIMIT_DELTA should be around the maximum expected number |
| 128 |
// of internal frames, and not too big because capturing the stack trace |
| 129 |
// is slow (this is why Error.stackTraceLimit does not default to Infinity!). |
| 130 |
// Increase it if needed. |
| 131 |
// However, we only do it if the user did not explicitly set it to 0. |
| 132 |
const MIN_STACK_TRACE_LIMIT = 50; |
| 133 |
Error.stackTraceLimit &&= Math.max( |
| 134 |
Error.stackTraceLimit, |
| 135 |
MIN_STACK_TRACE_LIMIT, |
| 136 |
); |
| 137 |
|
| 138 |
Error.prepareStackTrace = function stackTraceRewriter(err, trace) { |
| 139 |
let newTrace = []; |
| 140 |
|
| 141 |
const isExpected = expectedErrors.has(err); |
| 142 |
let status: "showing" | "hiding" | "unknown" = isExpected |
| 143 |
? "hiding" |
| 144 |
: "unknown"; |
| 145 |
for (let i = 0; i < trace.length; i++) { |
| 146 |
const name = trace[i].getFunctionName(); |
| 147 |
if (name === START_HIDNG) { |
| 148 |
status = "hiding"; |
| 149 |
} else if (name === STOP_HIDNG) { |
| 150 |
if (status === "hiding") { |
| 151 |
status = "showing"; |
| 152 |
if (virtualFrames.has(err)) { |
| 153 |
newTrace.unshift(...virtualFrames.get(err)); |
| 154 |
} |
| 155 |
} else if (status === "unknown") { |
| 156 |
// Unexpected internal error, show the full stack trace |
| 157 |
newTrace = trace; |
| 158 |
break; |
| 159 |
} |
| 160 |
} else if (status !== "hiding") { |
| 161 |
newTrace.push(trace[i]); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return prepareStackTrace(err, newTrace); |
| 166 |
}; |
| 167 |
} |
| 168 |
|
| 169 |
function defaultPrepareStackTrace(err: Error, trace: CallSite[]) { |
| 170 |
if (trace.length === 0) return ErrorToString(err); |
| 171 |
return `${ErrorToString(err)}\n at ${trace.join("\n at ")}`; |
| 172 |
} |