Skip to content

Commit

Permalink
feat: filter stack by path
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Dec 29, 2020
1 parent f93e942 commit 0aa7af6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions source/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,20 @@ function renderSource(
* @param error the error to be rendered
* @param options optional parameters
* @param options.showSource indicate whether a source frame should be shown
* @param options.filter a filter determining whether a stack should be shown given the file path
* @returns a rendered string to print
*/
export function renderStack(
error: Error,
options?: {
showSource?: boolean;
filter?: (path: string) => boolean;
},
): string {
const { showSource = false } = { ...options };
const {
showSource = false,
filter = (path: string) => !path.includes('node:internal'),
} = { ...options };

const blocks = disassembleStack(error.stack!);

Expand All @@ -190,15 +195,15 @@ export function renderStack(
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];

if (block.type === 'location') {
if (block.type === 'location' && filter(block.path)) {
renderedBlocks.push(
renderLocation(block, {
showSource:
// NOTE a location block must follow a description block
showSource && blocks[i - 1].type === 'description',
}),
);
} else {
} else if (block.type === 'description') {
renderedBlocks.push(renderDescription(block, currentError));
currentError = error['cause'];
}
Expand Down
47 changes: 47 additions & 0 deletions spec/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,53 @@ describe('fn:renderStack', () => {
);
});

it('renders an error stack without node:internal by default', () => {
const rendered = renderStack(
new MockedError(
'Error1: message1\n' +
' at entry1 (src1:1:0)\n' +
' at third_party (./node_modules/third_party/src:1:0)\n' +
' at internal (node:internal/modules/cjs/helper:1:0)\n' +
'Error2: message2\n' +
' at entry2 (src2:2:0)',
),
);

const plain = rendered.replace(ansiExpression, '');

expect(plain).toEqual(
'[Error1] message1\n' +
' at entry1 (src1:1:0)\n' +
' at third_party (./node_modules/third_party/src:1:0)\n' +
'[Error2] message2\n' +
' at entry2 (src2:2:0)',
);
});

it('renders an error stack according to the supplued filter', () => {
const rendered = renderStack(
new MockedError(
'Error1: message1\n' +
' at entry1 (src1:1:0)\n' +
' at third_party (./node_modules/third_party/src:1:0)\n' +
' at internal (node:internal/modules/cjs/helper:1:0)\n' +
'Error2: message2\n' +
' at entry2 (src2:2:0)',
),
{ filter: (path: string) => !path.includes('node_modules') },
);

const plain = rendered.replace(ansiExpression, '');

expect(plain).toEqual(
'[Error1] message1\n' +
' at entry1 (src1:1:0)\n' +
' at internal (node:internal/modules/cjs/helper:1:0)\n' +
'[Error2] message2\n' +
' at entry2 (src2:2:0)',
);
});

it('renders an error stack with the source', () => {
const rendered = renderStack(
new MockedError(
Expand Down

0 comments on commit 0aa7af6

Please sign in to comment.