Skip to content

Commit 1f99474

Browse files
fix: error handling better (#1141)
1 parent daf62be commit 1f99474

File tree

5 files changed

+85
-100
lines changed

5 files changed

+85
-100
lines changed

src/SassError.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class SassError extends Error {
77
// Instruct webpack to hide the JS stack from the console.
88
// Usually you're only interested in the SASS error in this case.
99
this.hideStack = true;
10-
Error.captureStackTrace(this, this.constructor);
1110

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

2726
if (sassError.formatted) {
28-
this.message = `${this.name}: ${sassError.formatted.replace(
29-
/^Error: /,
30-
""
31-
)}`;
27+
this.message = sassError.formatted.replace(/^Error: /, "");
3228
}
3329
}
3430
}

src/index.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import SassError from "./SassError";
2121
async function loader(content) {
2222
const options = this.getOptions(schema);
2323
const callback = this.async();
24-
const implementation = getSassImplementation(this, options.implementation);
2524

26-
if (!implementation) {
27-
callback();
25+
let implementation;
26+
27+
try {
28+
implementation = getSassImplementation(this, options.implementation);
29+
} catch (error) {
30+
callback(error);
2831

2932
return;
3033
}
@@ -59,7 +62,14 @@ async function loader(content) {
5962
}
6063
}
6164

62-
const compile = getCompileFn(implementation, options);
65+
let compile;
66+
67+
try {
68+
compile = getCompileFn(implementation, options);
69+
} catch (error) {
70+
callback(error);
71+
return;
72+
}
6373

6474
let result;
6575

src/utils.js

+6-27
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,24 @@ function getSassImplementation(loaderContext, implementation) {
3636
let resolvedImplementation = implementation;
3737

3838
if (!resolvedImplementation) {
39-
try {
40-
resolvedImplementation = getDefaultSassImplementation();
41-
} catch (error) {
42-
loaderContext.emitError(error);
43-
44-
return;
45-
}
39+
resolvedImplementation = getDefaultSassImplementation();
4640
}
4741

4842
if (typeof resolvedImplementation === "string") {
49-
try {
50-
// eslint-disable-next-line import/no-dynamic-require, global-require
51-
resolvedImplementation = require(resolvedImplementation);
52-
} catch (error) {
53-
loaderContext.emitError(error);
54-
55-
// eslint-disable-next-line consistent-return
56-
return;
57-
}
43+
// eslint-disable-next-line import/no-dynamic-require, global-require
44+
resolvedImplementation = require(resolvedImplementation);
5845
}
5946

6047
const { info } = resolvedImplementation;
6148

6249
if (!info) {
63-
loaderContext.emitError(new Error("Unknown Sass implementation."));
64-
65-
return;
50+
throw new Error("Unknown Sass implementation.");
6651
}
6752

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

7055
if (infoParts.length < 2) {
71-
loaderContext.emitError(
72-
new Error(`Unknown Sass implementation "${info}".`)
73-
);
74-
75-
return;
56+
throw new Error(`Unknown Sass implementation "${info}".`);
7657
}
7758

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

91-
loaderContext.emitError(
92-
new Error(`Unknown Sass implementation "${implementationName}".`)
93-
);
72+
throw new Error(`Unknown Sass implementation "${implementationName}".`);
9473
}
9574

9675
/**

test/__snapshots__/implementation-option.test.js.snap

+10-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ exports[`implementation option not specify: warnings 1`] = `[]`;
3838

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

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

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

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

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

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

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

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

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

8484
exports[`implementation option should try to load using valid order: errors 1`] = `
8585
[
86-
"ModuleError: Module Error (from ../src/cjs.js):
86+
"ModuleBuildError: Module build failed (from ../src/cjs.js):
8787
Some error sass",
8888
]
8989
`;

0 commit comments

Comments
 (0)