Skip to content

Commit 1baf909

Browse files
authored
feat: expose hideInternalStackFrames and makeErrorForHideStackFrame (#40)
* feat: expose `hideInternalStackFrames` and `makeErrorForHideStackFrame` * fix: test
1 parent 80e2976 commit 1baf909

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

src/error.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ describe("APIError", () => {
3737
statusCode: 500
3838
}"
3939
`);
40-
const stack = (e as InstanceType<typeof APIError>).errorWithStack.stack;
40+
const stack = (e as InstanceType<typeof APIError>).errorStack;
4141
expect(stack).toMatch(
4242
new RegExp(
43-
"ErrorWithStack:\\s*\\n" +
43+
"APIError:\\s*\\n" +
4444
"\\s+at testError \\(.*/error\\.test\\.ts:\\d+:\\d+\\)\\n" +
4545
"\\s+at deepTestError \\(.*/error\\.test\\.ts:\\d+:\\d+\\)",
4646
"s",

src/error.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@ function isErrorStackTraceLimitWritable() {
1010
: desc.set !== undefined;
1111
}
1212

13-
class ErrorWithStack extends Error {
14-
constructor() {
15-
super();
16-
this.name = "ErrorWithStack";
17-
}
18-
}
19-
2013
/**
2114
* Hide internal stack frames from the error stack trace.
2215
*/
23-
function hideInternalStackFrames(stack: string): string {
16+
export function hideInternalStackFrames(stack: string): string {
2417
const lines = stack.split("\n at ");
2518
if (lines.length <= 1) {
2619
return stack;
@@ -30,14 +23,17 @@ function hideInternalStackFrames(stack: string): string {
3023
}
3124

3225
// https://github.com/nodejs/node/blob/360f7cc7867b43344aac00564286b895e15f21d7/lib/internal/errors.js#L411-L432
33-
function makeErrorForHideStackFrame<B extends new (...args: any[]) => Error>(
26+
/**
27+
* Creates a custom error class that hides stack frames.
28+
*/
29+
export function makeErrorForHideStackFrame<B extends new (...args: any[]) => Error>(
3430
Base: B,
3531
clazz: any,
3632
): {
37-
new (...args: ConstructorParameters<B>): InstanceType<B> & { errorWithStack: ErrorWithStack };
33+
new (...args: ConstructorParameters<B>): InstanceType<B> & { errorStack: string | undefined };
3834
} {
3935
class HideStackFramesError extends Base {
40-
#errorWithStack: ErrorWithStack;
36+
#hiddenStack: string | undefined;
4137

4238
constructor(...args: any[]) {
4339
if (isErrorStackTraceLimitWritable()) {
@@ -48,13 +44,15 @@ function makeErrorForHideStackFrame<B extends new (...args: any[]) => Error>(
4844
} else {
4945
super(...args);
5046
}
51-
this.#errorWithStack = new ErrorWithStack();
52-
this.#errorWithStack.stack = hideInternalStackFrames(this.#errorWithStack.stack ?? "");
47+
const stack = new Error().stack;
48+
if (stack) {
49+
this.#hiddenStack = hideInternalStackFrames(stack.replace(/^Error/, this.name));
50+
}
5351
}
5452

5553
// use `getter` here to avoid the stack trace being captured by loggers
56-
get errorWithStack() {
57-
return this.#errorWithStack;
54+
get errorStack() {
55+
return this.#hiddenStack;
5856
}
5957

6058
// This is a workaround for wpt tests that expect that the error

0 commit comments

Comments
 (0)