Skip to content

Commit

Permalink
fix: error handling better (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 25, 2023
1 parent daf62be commit 1f99474
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 100 deletions.
6 changes: 1 addition & 5 deletions src/SassError.js
Expand Up @@ -7,7 +7,6 @@ class SassError extends Error {
// Instruct webpack to hide the JS stack from the console.
// Usually you're only interested in the SASS error in this case.
this.hideStack = true;
Error.captureStackTrace(this, this.constructor);

if (
typeof sassError.line !== "undefined" ||
Expand All @@ -25,10 +24,7 @@ class SassError extends Error {
}`;

if (sassError.formatted) {
this.message = `${this.name}: ${sassError.formatted.replace(
/^Error: /,
""
)}`;
this.message = sassError.formatted.replace(/^Error: /, "");
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/index.js
Expand Up @@ -21,10 +21,13 @@ import SassError from "./SassError";
async function loader(content) {
const options = this.getOptions(schema);
const callback = this.async();
const implementation = getSassImplementation(this, options.implementation);

if (!implementation) {
callback();
let implementation;

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

return;
}
Expand Down Expand Up @@ -59,7 +62,14 @@ async function loader(content) {
}
}

const compile = getCompileFn(implementation, options);
let compile;

try {
compile = getCompileFn(implementation, options);
} catch (error) {
callback(error);
return;
}

let result;

Expand Down
33 changes: 6 additions & 27 deletions src/utils.js
Expand Up @@ -36,43 +36,24 @@ function getSassImplementation(loaderContext, implementation) {
let resolvedImplementation = implementation;

if (!resolvedImplementation) {
try {
resolvedImplementation = getDefaultSassImplementation();
} catch (error) {
loaderContext.emitError(error);

return;
}
resolvedImplementation = getDefaultSassImplementation();
}

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

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

const { info } = resolvedImplementation;

if (!info) {
loaderContext.emitError(new Error("Unknown Sass implementation."));

return;
throw new Error("Unknown Sass implementation.");
}

const infoParts = info.split("\t");

if (infoParts.length < 2) {
loaderContext.emitError(
new Error(`Unknown Sass implementation "${info}".`)
);

return;
throw new Error(`Unknown Sass implementation "${info}".`);
}

const [implementationName] = infoParts;
Expand All @@ -88,9 +69,7 @@ function getSassImplementation(loaderContext, implementation) {
return resolvedImplementation;
}

loaderContext.emitError(
new Error(`Unknown Sass implementation "${implementationName}".`)
);
throw new Error(`Unknown Sass implementation "${implementationName}".`);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions test/__snapshots__/implementation-option.test.js.snap
Expand Up @@ -38,7 +38,7 @@ exports[`implementation option not specify: warnings 1`] = `[]`;

exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
[
"ModuleError: Module Error (from ../src/cjs.js):
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Some error",
]
`;
Expand All @@ -47,43 +47,43 @@ exports[`implementation option should not swallow an error when trying to load a

exports[`implementation option should throw an error on an unknown sass implementation: errors 1`] = `
[
"ModuleError: Module Error (from ../src/cjs.js):
Unknown Sass implementation "strange-sass".",
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Unknown Sass implementation "strange-sass".",
]
`;

exports[`implementation option should throw an error on an unknown sass implementation: warnings 1`] = `[]`;

exports[`implementation option should throw an error when the "info" is unparseable: errors 1`] = `
[
"ModuleError: Module Error (from ../src/cjs.js):
Unknown Sass implementation "asdfj".",
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Unknown Sass implementation "asdfj".",
]
`;

exports[`implementation option should throw an error when the "info" is unparseable: warnings 1`] = `[]`;

exports[`implementation option should throw error when the "info" does not exist: errors 1`] = `
[
"ModuleError: Module Error (from ../src/cjs.js):
Unknown Sass implementation.",
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Unknown Sass implementation.",
]
`;

exports[`implementation option should throw error when the "info" does not exist: warnings 1`] = `[]`;

exports[`implementation option should throw error when unresolved package: errors 1`] = `
[
"ModuleError: Module Error (from ../src/cjs.js):
Cannot find module 'unresolved' from 'src/utils.js'",
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Cannot find module 'unresolved' from 'src/utils.js'",
]
`;

exports[`implementation option should throw error when unresolved package: warnings 1`] = `[]`;

exports[`implementation option should try to load using valid order: errors 1`] = `
[
"ModuleError: Module Error (from ../src/cjs.js):
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Some error sass",
]
`;
Expand Down

0 comments on commit 1f99474

Please sign in to comment.