Skip to content

Commit

Permalink
Simplify formatSassError
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Dec 27, 2016
1 parent 09589c3 commit d8e1fa9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
46 changes: 22 additions & 24 deletions lib/formatSassError.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@ const SassError = { // eslint-disable-line no-unused-vars
/**
* Enhances the sass error with additional information about what actually went wrong.
*
* @param {SassError} err
* @param {string} resourcePath
* @returns {Function<SassError>}
*/
function formatSassError(resourcePath) {
return (err) => {
// Instruct webpack to hide the JS stack from the console
// Usually you're only interested in the SASS stack in this case.
err.hideStack = true;
function formatSassError(err, resourcePath) {
// Instruct webpack to hide the JS stack from the console
// Usually you're only interested in the SASS stack in this case.
err.hideStack = true;

// The file property is missing in rare cases.
// No improvement in the error is possible.
if (!err.file) {
return;
}
// The file property is missing in rare cases.
// No improvement in the error is possible.
if (!err.file) {
return;
}

let msg = err.message;
let msg = err.message;

if (err.file === "stdin") {
err.file = resourcePath;
}
// node-sass returns UNIX-style paths
err.file = path.normalize(err.file);
if (err.file === "stdin") {
err.file = resourcePath;
}
// node-sass returns UNIX-style paths
err.file = path.normalize(err.file);

// The 'Current dir' hint of node-sass does not help us, we're providing
// additional information by reading the err.file property
msg = msg.replace(/\s*Current dir:\s*/, "");
// The 'Current dir' hint of node-sass does not help us, we're providing
// additional information by reading the err.file property
msg = msg.replace(/\s*Current dir:\s*/, "");

err.message = getFileExcerptIfPossible(err) +
msg.charAt(0).toUpperCase() + msg.slice(1) + os.EOL +
" in " + err.file + " (line " + err.line + ", column " + err.column + ")";
};
err.message = getFileExcerptIfPossible(err) +
msg.charAt(0).toUpperCase() + msg.slice(1) + os.EOL +
" in " + err.file + " (line " + err.line + ", column " + err.column + ")";
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const asyncSassJobQueue = async.queue(sass.render, threadPoolSize - 1);
* @param {string} content
*/
function sassLoader(content) {
const formatSassErrorHere = formatSassError(this.resourcePath);
const callback = this.async();
const isSync = typeof callback !== "function";
const self = this;
Expand Down Expand Up @@ -104,7 +103,7 @@ function sassLoader(content) {
// start the actual rendering
asyncSassJobQueue.push(sassOptions, (err, result) => {
if (err) {
formatSassErrorHere(err);
formatSassError(err, this.resourcePath);
err.file && self.dependency(err.file);
callback(err);
return;
Expand Down

0 comments on commit d8e1fa9

Please sign in to comment.