Skip to content

Commit

Permalink
style: move public methods to the top of files
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Sep 20, 2023
1 parent ece5641 commit 08d4476
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions source/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,51 @@ const DEFAULT_YAML_THEME = {
undefined: chalk.yellow.bold,
};

/**
* render a highly readable error stack
* @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,
filter = (path: string) => !path.includes('node:internal'),
} = { ...options };

const blocks = disassembleStack(error.stack!);

let currentError: unknown = error;
const renderedBlocks: string[] = [];

for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];

if (block.type === 'description') {
renderedBlocks.push(renderDescription(block, currentError));
currentError = error['cause'];
} else if (filter(block.path)) {
renderedBlocks.push(
renderLocation(block, {
showSource:
// NOTE a location block must follow a description block
showSource && blocks[i - 1].type === 'description',
}),
);
}
}

return renderedBlocks.join('\n');
}

/**
* render associations of an error
* @param error the related error
Expand Down Expand Up @@ -167,48 +212,3 @@ function renderSource(

return '\n' + sourceFrame + '\n';
}

/**
* render a highly readable error stack
* @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,
filter = (path: string) => !path.includes('node:internal'),
} = { ...options };

const blocks = disassembleStack(error.stack!);

let currentError: unknown = error;
const renderedBlocks: string[] = [];

for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];

if (block.type === 'description') {
renderedBlocks.push(renderDescription(block, currentError));
currentError = error['cause'];
} else if (filter(block.path)) {
renderedBlocks.push(
renderLocation(block, {
showSource:
// NOTE a location block must follow a description block
showSource && blocks[i - 1].type === 'description',
}),
);
}
}

return renderedBlocks.join('\n');
}

0 comments on commit 08d4476

Please sign in to comment.