Skip to content

Commit

Permalink
Clean up error handling a little
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Jul 7, 2024
1 parent a6539dd commit 21a9485
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 7 additions & 3 deletions bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,15 @@ class PeggyCLI extends Command {
if (typeof mod.use !== "function") {
mod = mod.default;
}
if (typeof mod.use !== "function") {
this.error(`Invalid plugin "${id}", no \`use()\` function`);
}
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") {
this.error(`requiring:\n${e.stack}`);
if ((e.code === "ERR_MODULE_NOT_FOUND")
|| (e.code === "MODULE_NOT_FOUND")) {
this.error(`importing: ${e.message}`);
} else {
this.error(`requiring "${id}": ${e.message}`);
this.error(`importing "${id}":\n${e.stack}`);
}
}
return mod;
Expand Down
11 changes: 10 additions & 1 deletion test/cli/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ Options:
const plugin2Mjs = path.join(fixtures, "plugin2.mjs");
const pluginCjs = path.join(fixtures, "plugin.cjs");
const bad = path.join(fixtures, "bad.js");
const optFileJS = path.join(fixtures, "options.js");

await exec({
args: [
Expand Down Expand Up @@ -889,7 +890,15 @@ Options:
stdin: "foo = '1'",
errorCode: "peggy.invalidArgument",
exitCode: 1,
error: 'requiring "ERROR BAD MODULE DOES NOT EXIST"',
error: "Error importing: Cannot find module",
});

await exec({
args: ["--plugin", optFileJS],
stdin: "foo = '1'",
errorCode: "peggy.invalidArgument",
exitCode: 1,
error: "no `use()` function",
});

await exec({
Expand Down

0 comments on commit 21a9485

Please sign in to comment.