|
4 | 4 | */ |
5 | 5 | "use strict"; |
6 | 6 |
|
| 7 | +const createHash = require("./util/createHash"); |
| 8 | + |
| 9 | +const hash = Symbol(); |
| 10 | +const meta = Symbol(); |
| 11 | + |
7 | 12 | module.exports = class WebpackError extends Error { |
| 13 | + constructor( |
| 14 | + message, |
| 15 | + line = 0, |
| 16 | + column = 0, |
| 17 | + columnEnd = 0, |
| 18 | + category = "error" |
| 19 | + ) { |
| 20 | + // TODO: enable message assertion during construction in a commit to follow |
| 21 | + // if (!message) { |
| 22 | + // WebpackError.deprecate("message must be a non-null, non-empty value."); |
| 23 | + // } |
| 24 | + // TODO: enable message length limit at a commit to follow |
| 25 | + // else if (message.length > 160) { |
| 26 | + // WebpackError.deprecate("message cannot be longer than 80 characters."); |
| 27 | + // } |
| 28 | + |
| 29 | + super(message); |
| 30 | + |
| 31 | + this[hash] = createHash("md4"); |
| 32 | + this[meta] = {}; |
| 33 | + this.category = category; |
| 34 | + this.column = column; |
| 35 | + this.columnEnd = columnEnd; |
| 36 | + this.line = line; |
| 37 | + |
| 38 | + // TODO: enable abstract protection at a commit to follow |
| 39 | + // if (this.constructor === WebpackError) { |
| 40 | + // const message = `A WebpackError cannot be directly constructed.`; |
| 41 | + // WebpackError.deprecate(message); |
| 42 | + // } |
| 43 | + } |
| 44 | + |
| 45 | + static deprecate(message) { |
| 46 | + const err = new Error(); |
| 47 | + const stack = err.stack |
| 48 | + .split("\n") |
| 49 | + .slice(3, 4) |
| 50 | + .join("\n"); |
| 51 | + // use process.stdout to assert the message will be displayed in |
| 52 | + // environments where console has been proxied. this mimics node's |
| 53 | + // util.deprecate method. |
| 54 | + process.stdout.write(`DeprecationWarning: ${message}\n${stack}\n`); |
| 55 | + } |
| 56 | + |
| 57 | + get id() { |
| 58 | + return this[hash]; |
| 59 | + } |
| 60 | + |
8 | 61 | inspect() { |
9 | 62 | return this.stack + (this.details ? `\n${this.details}` : ""); |
10 | 63 | } |
| 64 | + |
| 65 | + get meta() { |
| 66 | + return this[meta]; |
| 67 | + } |
| 68 | + |
| 69 | + // TODO: enable this standard output in a later PR. at present, webpack tests |
| 70 | + // rely heavily on arbitrary toString() output of different errors. |
| 71 | + // toString(formatFn) { |
| 72 | + // if (formatFn) { |
| 73 | + // return formatFn(this); |
| 74 | + // } |
| 75 | + // |
| 76 | + // let preface = `${this.line}:${this.column} `; |
| 77 | + // |
| 78 | + // if (this.category) { |
| 79 | + // preface += `${this.category}: `; |
| 80 | + // } |
| 81 | + // |
| 82 | + // return `${preface}${this.message}`; |
| 83 | + // } |
11 | 84 | }; |
0 commit comments