Skip to content

Commit

Permalink
fix: make errors serializable (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 28, 2023
1 parent 5e0308e commit 68adcc2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 42 deletions.
2 changes: 1 addition & 1 deletion 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"],
};
39 changes: 0 additions & 39 deletions src/LessError.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -6,8 +6,8 @@ import {
isUnsupportedUrl,
normalizeSourceMap,
getLessImplementation,
errorFactory,
} from "./utils";
import LessError from "./LessError";

async function lessLoader(source) {
const options = this.getOptions(schema);
Expand Down Expand Up @@ -78,7 +78,7 @@ async function lessLoader(source) {
this.addDependency(path.normalize(error.filename));
}

callback(new LessError(error));
callback(errorFactory(error));

return;
} finally {
Expand Down
37 changes: 37 additions & 0 deletions src/utils.js
Expand Up @@ -240,9 +240,46 @@ function getLessImplementation(loaderContext, implementation) {
return resolvedImplementation;
}

function getFileExcerptIfPossible(error) {
if (typeof error.extract === "undefined") {
return [];
}

const excerpt = error.extract.slice(0, 2);
const column = Math.max(error.column - 1, 0);

if (typeof excerpt[0] === "undefined") {
excerpt.shift();
}

excerpt.push(`${new Array(column).join(" ")}^`);

return excerpt;
}

function errorFactory(error) {
const message = [
"\n",
...getFileExcerptIfPossible(error),
error.message.charAt(0).toUpperCase() + error.message.slice(1),
error.filename
? ` Error in ${path.normalize(error.filename)} (line ${
error.line
}, column ${error.column})`
: "",
].join("\n");

const obj = new Error(message, { cause: error });

obj.stack = null;

return obj;
}

export {
getLessOptions,
isUnsupportedUrl,
normalizeSourceMap,
getLessImplementation,
errorFactory,
};
9 changes: 9 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -431,6 +431,15 @@ exports[`loader should resolve unresolved url with alias: errors 1`] = `[]`;

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

exports[`loader should throw an error: errors 1`] = `
[
"ModuleBuildError: Module build failed (from \`replaced original path\`):
",
]
`;

exports[`loader should throw an error: warnings 1`] = `[]`;

exports[`loader should transform urls: css 1`] = `
".img4 {
background: url(folder/img.jpg);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/broken.less
@@ -0,0 +1 @@
broken;
9 changes: 9 additions & 0 deletions test/loader.test.js
Expand Up @@ -991,4 +991,13 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should throw an error", async () => {
const testId = "./broken.less";
const compiler = getCompiler(testId);
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});

0 comments on commit 68adcc2

Please sign in to comment.