Skip to content

Commit 68adcc2

Browse files
fix: make errors serializable (#516)
1 parent 5e0308e commit 68adcc2

File tree

7 files changed

+59
-42
lines changed

7 files changed

+59
-42
lines changed

lint-staged.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
"*": ["prettier --write --ignore-unknown", "cspell"],
2+
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
33
"*.{js}": ["eslint --cache --fix"],
44
};

src/LessError.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
isUnsupportedUrl,
77
normalizeSourceMap,
88
getLessImplementation,
9+
errorFactory,
910
} from "./utils";
10-
import LessError from "./LessError";
1111

1212
async function lessLoader(source) {
1313
const options = this.getOptions(schema);
@@ -78,7 +78,7 @@ async function lessLoader(source) {
7878
this.addDependency(path.normalize(error.filename));
7979
}
8080

81-
callback(new LessError(error));
81+
callback(errorFactory(error));
8282

8383
return;
8484
} finally {

src/utils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,46 @@ function getLessImplementation(loaderContext, implementation) {
240240
return resolvedImplementation;
241241
}
242242

243+
function getFileExcerptIfPossible(error) {
244+
if (typeof error.extract === "undefined") {
245+
return [];
246+
}
247+
248+
const excerpt = error.extract.slice(0, 2);
249+
const column = Math.max(error.column - 1, 0);
250+
251+
if (typeof excerpt[0] === "undefined") {
252+
excerpt.shift();
253+
}
254+
255+
excerpt.push(`${new Array(column).join(" ")}^`);
256+
257+
return excerpt;
258+
}
259+
260+
function errorFactory(error) {
261+
const message = [
262+
"\n",
263+
...getFileExcerptIfPossible(error),
264+
error.message.charAt(0).toUpperCase() + error.message.slice(1),
265+
error.filename
266+
? ` Error in ${path.normalize(error.filename)} (line ${
267+
error.line
268+
}, column ${error.column})`
269+
: "",
270+
].join("\n");
271+
272+
const obj = new Error(message, { cause: error });
273+
274+
obj.stack = null;
275+
276+
return obj;
277+
}
278+
243279
export {
244280
getLessOptions,
245281
isUnsupportedUrl,
246282
normalizeSourceMap,
247283
getLessImplementation,
284+
errorFactory,
248285
};

test/__snapshots__/loader.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ exports[`loader should resolve unresolved url with alias: errors 1`] = `[]`;
431431

432432
exports[`loader should resolve unresolved url with alias: warnings 1`] = `[]`;
433433

434+
exports[`loader should throw an error: errors 1`] = `
435+
[
436+
"ModuleBuildError: Module build failed (from \`replaced original path\`):
437+
",
438+
]
439+
`;
440+
441+
exports[`loader should throw an error: warnings 1`] = `[]`;
442+
434443
exports[`loader should transform urls: css 1`] = `
435444
".img4 {
436445
background: url(folder/img.jpg);

test/fixtures/broken.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
broken;

test/loader.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,4 +991,13 @@ describe("loader", () => {
991991
expect(getWarnings(stats)).toMatchSnapshot("warnings");
992992
expect(getErrors(stats)).toMatchSnapshot("errors");
993993
});
994+
995+
it("should throw an error", async () => {
996+
const testId = "./broken.less";
997+
const compiler = getCompiler(testId);
998+
const stats = await compile(compiler);
999+
1000+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1001+
expect(getErrors(stats)).toMatchSnapshot("errors");
1002+
});
9941003
});

0 commit comments

Comments
 (0)