Skip to content

Commit

Permalink
fix: handling errors better (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 25, 2023
1 parent 5cdb7ea commit 25de46d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
"*": ["prettier --write --ignore-unknown", "cspell"],
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
"*.js": ["eslint --cache --fix"],
};
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ import {
export default async function stylusLoader(source) {
const options = this.getOptions(schema);
const callback = this.async();
const implementation = getStylusImplementation(this, options.implementation);

let implementation;

try {
implementation = getStylusImplementation(this, options.implementation);
} catch (error) {
callback(error);

return;
}

if (!implementation) {
callback();
callback(
new Error(
`The Stylus implementation "${options.implementation}" not found`
)
);

return;
}
Expand Down
11 changes: 2 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,8 @@ function getStylusImplementation(loaderContext, implementation) {
if (!implementation || typeof implementation === "string") {
const stylusImplPkg = implementation || "stylus";

try {
// eslint-disable-next-line import/no-dynamic-require, global-require
resolvedImplementation = require(stylusImplPkg);
} catch (error) {
loaderContext.emitError(error);

// eslint-disable-next-line consistent-return
return;
}
// eslint-disable-next-line import/no-dynamic-require, global-require
resolvedImplementation = require(stylusImplPkg);
}

// eslint-disable-next-line consistent-return
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/implementation-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

exports[`implementation option should throw error when unresolved package: errors 1`] = `
[
"ModuleError: Module Error (from \`replaced original path\`):
(Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'",
"ModuleBuildError: Module build failed (from \`replaced original path\`):
NonErrorEmittedError: (Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'",
]
`;

Expand Down

0 comments on commit 25de46d

Please sign in to comment.