Skip to content

Commit ccef136

Browse files
committed
fix(error): wrap and re-throw errors
1 parent 0c04871 commit ccef136

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

bin/cli.mjs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ const program = new Command()
1111
.name('api-docs-tooling')
1212
.description('CLI tool to generate and lint Node.js API documentation');
1313

14+
/**
15+
* Returns a wrapped version of the given async function that catches and rethrows any errors.
16+
*
17+
* @function
18+
* @param {Function} fn - The async function to wrap.
19+
* @returns {Function} A new function that calls `fn` with any given arguments and rethrows errors.
20+
*/
21+
const errorWrap =
22+
fn =>
23+
(...args) =>
24+
fn(...args).catch(e => {
25+
console.error(e);
26+
process.exit(1);
27+
});
28+
1429
// Registering generate and lint commands
1530
commands.forEach(({ name, description, options, action }) => {
1631
const cmd = program.command(name).description(description);
@@ -33,21 +48,21 @@ commands.forEach(({ name, description, options, action }) => {
3348
});
3449

3550
// Set the action for the command
36-
cmd.action(action);
51+
cmd.action(errorWrap(action));
3752
});
3853

3954
// Register the interactive command
4055
program
4156
.command('interactive')
4257
.description('Launch guided CLI wizard')
43-
.action(interactive);
58+
.action(errorWrap(interactive));
4459

4560
// Register the list command
4661
program
4762
.command('list')
4863
.addArgument(new Argument('<types>', 'The type to list').choices(types))
4964
.description('List the given type')
50-
.action(list);
65+
.action(errorWrap(list));
5166

5267
// Parse and execute command-line arguments
5368
program.parse(process.argv);

src/threading/index.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ export default class WorkerPool {
5656
this.changeActiveThreadCount(-1);
5757
this.processQueue(threads);
5858

59-
(result?.error ? reject : resolve)(result);
59+
if (result?.error) {
60+
reject(result.error);
61+
} else {
62+
resolve(result);
63+
}
6064
});
6165

6266
// Handle worker thread errors

0 commit comments

Comments
 (0)