Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make errors serializable #516

Merged
merged 2 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should improve cause on webpack side too, in my TODO


obj.stack = null;

return obj;
}

export {
getLessOptions,
isUnsupportedUrl,
normalizeSourceMap,
getLessImplementation,
errorFactory,
};
9 changes: 9 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
broken;
9 changes: 9 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
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");
});
});