Skip to content

Commit

Permalink
feat: add a showStack option to renderError
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Oct 20, 2023
1 parent 32bdc5e commit 41d47c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion source/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface RenderOptions {
indent?: string;
/** indicate whether a source frame should be shown */
showSource?: boolean;
/** indicate whether the full stack should be shown */
showStack?: boolean;
/** a filter function determining whether a stack should be shown given the file path */
filter?: (path: string) => boolean;
}
Expand Down Expand Up @@ -78,6 +80,7 @@ export function renderError(error: Error, options?: RenderOptions): string {
const {
indent = '',
showSource = false,
showStack = true,
filter = (path: string) =>
!path.includes('node:internal') && !path.includes('node_modules'),
} = { ...options };
Expand All @@ -86,7 +89,7 @@ export function renderError(error: Error, options?: RenderOptions): string {

const locations = disassembleStack(stack).filter(
(block): block is StackLocationBlock =>
block.type === 'location' && filter(block.path),
showStack && block.type === 'location' && filter(block.path),
);

const renderedBlocks: string[] = [
Expand Down
15 changes: 15 additions & 0 deletions spec/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ describe('fn:renderError', () => {
);
});

it('should skip the stack if instructed', () => {
const rendered = renderError(
new MockedError(
'Error1: message1\n' +
' at entry1 (src1:1:0)\n' +
' at entry2 (src2:2:0)',
),
{ showStack: false },
);

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

expect(plain).toEqual('[Error1] message1');
});

it('should render an error stack without node:internal & node_modules by default', () => {
const rendered = renderError(
new MockedError(
Expand Down

0 comments on commit 41d47c2

Please sign in to comment.