|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -function ModuleParseError(module, source, err) { |
6 | | - Error.call(this); |
7 | | - Error.captureStackTrace(this, ModuleParseError); |
8 | | - this.name = "ModuleParseError"; |
9 | | - this.message = "Module parse failed: " + module.request + " " + err.message; |
10 | | - this.message += "\nYou may need an appropriate loader to handle this file type."; |
11 | | - if(err.loc && typeof err.loc === "object" && typeof err.loc.line === "number") { |
12 | | - var lineNumber = err.loc.line; |
13 | | - if(/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) { // binary file |
14 | | - this.message += "\n(Source code omitted for this binary file)"; |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +class ModuleParseError extends Error { |
| 8 | + constructor(module, source, err) { |
| 9 | + super(); |
| 10 | + if(Error.hasOwnProperty("captureStackTrace")) { |
| 11 | + Error.captureStackTrace(this, this.constructor); |
| 12 | + } |
| 13 | + this.name = "ModuleParseError"; |
| 14 | + this.message = "Module parse failed: " + module.request + " " + err.message; |
| 15 | + this.message += "\nYou may need an appropriate loader to handle this file type."; |
| 16 | + if(err.loc && typeof err.loc === "object" && typeof err.loc.line === "number") { |
| 17 | + var lineNumber = err.loc.line; |
| 18 | + if(/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) { // binary file |
| 19 | + this.message += "\n(Source code omitted for this binary file)"; |
| 20 | + } else { |
| 21 | + source = source.split("\n"); |
| 22 | + this.message += "\n| " + source.slice(Math.max(0, lineNumber - 3), lineNumber + 2).join("\n| "); |
| 23 | + } |
15 | 24 | } else { |
16 | | - source = source.split("\n"); |
17 | | - this.message += "\n| " + source.slice(Math.max(0, lineNumber - 3), lineNumber + 2).join("\n| "); |
| 25 | + this.message += "\n" + err.stack; |
18 | 26 | } |
19 | | - } else { |
20 | | - this.message += "\n" + err.stack; |
| 27 | + this.module = module; |
| 28 | + this.error = err; |
21 | 29 | } |
22 | | - this.module = module; |
23 | | - this.error = err; |
24 | 30 | } |
25 | | -module.exports = ModuleParseError; |
26 | 31 |
|
27 | | -ModuleParseError.prototype = Object.create(Error.prototype); |
28 | | -ModuleParseError.prototype.constructor = ModuleParseError; |
| 32 | +module.exports = ModuleParseError; |
0 commit comments