Skip to content

Commit

Permalink
fix: make errors serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 27, 2023
1 parent 5e0308e commit 2a8ba54
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
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 [];

Check warning on line 245 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L245

Added line #L245 was not covered by tests
}

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})`
: "",

Check warning on line 269 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L269

Added line #L269 was not covered by tests
].join("\n");

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

obj.stack = null;

return obj;
}

export {
getLessOptions,
isUnsupportedUrl,
normalizeSourceMap,
getLessImplementation,
errorFactory,
};

0 comments on commit 2a8ba54

Please sign in to comment.