Skip to content

Commit b6cf072

Browse files
committed
handle errors sync and async
1 parent ccef136 commit b6cf072

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

bin/cli.mjs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ const program = new Command()
1212
.description('CLI tool to generate and lint Node.js API documentation');
1313

1414
/**
15-
* Returns a wrapped version of the given async function that catches and rethrows any errors.
15+
* Wraps a function to catch both synchronous and asynchronous errors.
1616
*
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.
17+
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
18+
* @returns {Function} A new function that handles errors and logs them.
2019
*/
2120
const errorWrap =
2221
fn =>
23-
(...args) =>
24-
fn(...args).catch(e => {
25-
console.error(e);
22+
async (...args) => {
23+
try {
24+
return await fn(...args);
25+
} catch (err) {
26+
console.error(err);
2627
process.exit(1);
27-
});
28+
}
29+
};
2830

2931
// Registering generate and lint commands
3032
commands.forEach(({ name, description, options, action }) => {

0 commit comments

Comments
 (0)