Skip to content

Commit

Permalink
refactor(src/Error): move LoaderError to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky authored and danez committed Feb 25, 2018
1 parent 19caf69 commit 552bdce
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const STRIP_FILENAME_RE = /^[^:]+: /;

const format = err => {
if (err instanceof SyntaxError) {
err.name = "SyntaxError";
err.message = err.message.replace(STRIP_FILENAME_RE, "");

err.hideStack = true;
} else if (err instanceof TypeError) {
err.name = null;
err.message = err.message.replace(STRIP_FILENAME_RE, "");

err.hideStack = true;
}

return err;
};

class LoaderError extends Error {
constructor(err) {
super();

const { name, message, codeFrame, hideStack } = format(err);

this.name = "BabelLoaderError";

this.message = `${name ? `${name}: ` : ""}${message}\n\n${codeFrame}\n`;

this.hideStack = hideStack;

Error.captureStackTrace(this, this.constructor);
}
}

module.exports = LoaderError;

0 comments on commit 552bdce

Please sign in to comment.