From 25de46dcff4a9d989a706fc198919f0e5cf07efa Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 25 May 2023 23:16:38 +0300 Subject: [PATCH] fix: handling errors better (#390) --- lint-staged.config.js | 2 +- src/index.js | 17 +++++++++++++++-- src/utils.js | 11 ++--------- .../implementation-option.test.js.snap | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lint-staged.config.js b/lint-staged.config.js index 4e51a30..372e7ff 100644 --- a/lint-staged.config.js +++ b/lint-staged.config.js @@ -1,4 +1,4 @@ module.exports = { - "*": ["prettier --write --ignore-unknown", "cspell"], + "*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"], "*.js": ["eslint --cache --fix"], }; diff --git a/src/index.js b/src/index.js index b087ea5..d78f58e 100644 --- a/src/index.js +++ b/src/index.js @@ -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; } diff --git a/src/utils.js b/src/utils.js index 737f99d..533c0b7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 diff --git a/test/__snapshots__/implementation-option.test.js.snap b/test/__snapshots__/implementation-option.test.js.snap index 2b036e5..e9ccfd7 100644 --- a/test/__snapshots__/implementation-option.test.js.snap +++ b/test/__snapshots__/implementation-option.test.js.snap @@ -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'", ] `;