Skip to content

fix(error): wrap and re-throw errors #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Argument, Command, Option } from 'commander';
import interactive from './commands/interactive.mjs';
import list, { types } from './commands/list.mjs';
import commands from './commands/index.mjs';
import { errorWrap } from './utils.mjs';

const program = new Command()
.name('api-docs-tooling')
Expand Down Expand Up @@ -33,21 +34,21 @@ commands.forEach(({ name, description, options, action }) => {
});

// Set the action for the command
cmd.action(action);
cmd.action(errorWrap(action));
});

// Register the interactive command
program
.command('interactive')
.description('Launch guided CLI wizard')
.action(interactive);
.action(errorWrap(interactive));

// Register the list command
program
.command('list')
.addArgument(new Argument('<types>', 'The type to list').choices(types))
.description('List the given type')
.action(list);
.action(errorWrap(list));

// Parse and execute command-line arguments
program.parse(process.argv);
41 changes: 36 additions & 5 deletions bin/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import createMarkdownLoader from '../src/loaders/markdown.mjs';
import createMarkdownParser from '../src/parsers/markdown.mjs';

// Instantiate loader and parser once to reuse
const loader = createMarkdownLoader();
const parser = createMarkdownParser();
/**
* Generic lazy initializer.
* @template T
* @param {() => T} factory - Function to create the instance.
* @returns {() => T} - A function that returns the singleton instance.
*/
export const lazy = factory => {
let instance;
return () => (instance ??= factory());
};

// Instantiate loader and parser once to reuse,
// but only if/when we actually need them. No need
// to create these objects just to load a different
// utility.
const loader = lazy(createMarkdownLoader);
const parser = lazy(createMarkdownParser);

/**
* Load and parse markdown API docs.
Expand All @@ -12,10 +26,27 @@ const parser = createMarkdownParser();
* @returns {Promise<ApiDocMetadataEntry[]>} - Parsed documentation objects.
*/
export async function loadAndParse(input, ignore) {
const files = await loader.loadFiles(input, ignore);
return parser.parseApiDocs(files);
const files = await loader().loadFiles(input, ignore);
return parser().parseApiDocs(files);
}

/**
* Wraps a function to catch both synchronous and asynchronous errors.
*
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
* @returns {Function} A new function that handles errors and logs them.
*/
export const errorWrap =
fn =>
async (...args) => {
try {
return await fn(...args);
} catch (err) {
console.error(err);
process.exit(1);
}
};

/**
* Represents a command-line option for the linter CLI.
* @typedef {Object} Option
Expand Down
6 changes: 5 additions & 1 deletion src/threading/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export default class WorkerPool {
this.changeActiveThreadCount(-1);
this.processQueue(threads);

(result?.error ? reject : resolve)(result);
if (result?.error) {
reject(result.error);
} else {
resolve(result);
}
});

// Handle worker thread errors
Expand Down
Loading