Skip to content

Commit ff7a4c8

Browse files
refactor: SyntaxError class
1 parent 7570df9 commit ff7a4c8

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

lib/CssLoaderError.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/SyntaxError.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const formatCodeFrame = require("babel-code-frame");
2+
3+
class SyntaxError extends Error {
4+
constructor(err) {
5+
super(err);
6+
7+
this.name = this.constructor.name;
8+
this.message = err.reason ? err.reason : err.message;
9+
10+
if (err.line && err.column) {
11+
this.message += " (" + err.line + ":" + err.column + ")";
12+
13+
if (err.source) {
14+
this.message +=
15+
"\n\n" + formatCodeFrame(err.source, err.line, err.column) + "\n";
16+
}
17+
}
18+
19+
Error.captureStackTrace(this, this.constructor);
20+
}
21+
}
22+
23+
module.exports = SyntaxError;

lib/loader.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const loaderUtils = require("loader-utils");
66
const postcss = require("postcss");
77
const plugin = require("./plugin");
88
const getImportPrefix = require("./getImportPrefix");
9-
const CssLoaderError = require("./CssLoaderError");
9+
const SyntaxError = require("./SyntaxError");
1010

1111
module.exports = function(content, map) {
1212
const options = loaderUtils.getOptions(this) || {};
@@ -188,17 +188,6 @@ module.exports = function(content, map) {
188188
.catch(err => {
189189
// Todo if (err.file) this.addDependency(err.file)
190190

191-
cb(
192-
err.name === "CssSyntaxError"
193-
? new CssLoaderError(
194-
"Syntax Error",
195-
err.reason,
196-
err.line != null && err.column != null
197-
? { line: err.line, column: err.column }
198-
: null,
199-
err.input.source
200-
)
201-
: err
202-
);
191+
cb(err.name === "CssSyntaxError" ? new SyntaxError(err) : err);
203192
});
204193
};

0 commit comments

Comments
 (0)