|
12 | 12 |
|
13 | 13 | const kCode = Symbol('code');
|
14 | 14 | const messages = new Map();
|
| 15 | +const codes = {}; |
15 | 16 |
|
16 | 17 | var green = '';
|
17 | 18 | var red = '';
|
@@ -194,6 +195,54 @@ function createErrDiff(actual, expected, operator) {
|
194 | 195 | return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}`;
|
195 | 196 | }
|
196 | 197 |
|
| 198 | +function makeNodeErrorWithCode(Base, key) { |
| 199 | + return class NodeError extends Base { |
| 200 | + constructor(...args) { |
| 201 | + super(message(key, args)); |
| 202 | + } |
| 203 | + |
| 204 | + get name() { |
| 205 | + return `${super.name} [${key}]`; |
| 206 | + } |
| 207 | + |
| 208 | + set name(value) { |
| 209 | + defineProperty(this, 'name', { |
| 210 | + configurable: true, |
| 211 | + enumerable: true, |
| 212 | + value, |
| 213 | + writable: true |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + get code() { |
| 218 | + return key; |
| 219 | + } |
| 220 | + |
| 221 | + set code(value) { |
| 222 | + defineProperty(this, 'code', { |
| 223 | + configurable: true, |
| 224 | + enumerable: true, |
| 225 | + value, |
| 226 | + writable: true |
| 227 | + }); |
| 228 | + } |
| 229 | + }; |
| 230 | +} |
| 231 | + |
| 232 | +// Utility function for registering the error codes. Only used here. Exported |
| 233 | +// *only* to allow for testing. |
| 234 | +function E(sym, val, def, ...otherClasses) { |
| 235 | + messages.set(sym, val); |
| 236 | + if (def === undefined) return; |
| 237 | + def = makeNodeErrorWithCode(def, sym); |
| 238 | + if (otherClasses.length !== 0) { |
| 239 | + otherClasses.forEach((clazz) => { |
| 240 | + def[clazz.name] = makeNodeErrorWithCode(clazz, sym); |
| 241 | + }); |
| 242 | + } |
| 243 | + codes[sym] = def; |
| 244 | +} |
| 245 | + |
197 | 246 | class AssertionError extends Error {
|
198 | 247 | constructor(options) {
|
199 | 248 | if (typeof options !== 'object' || options === null) {
|
@@ -296,12 +345,6 @@ function message(key, args) {
|
296 | 345 | return String(fmt.apply(null, args));
|
297 | 346 | }
|
298 | 347 |
|
299 |
| -// Utility function for registering the error codes. Only used here. Exported |
300 |
| -// *only* to allow for testing. |
301 |
| -function E(sym, val) { |
302 |
| - messages.set(sym, typeof val === 'function' ? val : String(val)); |
303 |
| -} |
304 |
| - |
305 | 348 | /**
|
306 | 349 | * This used to be util._errnoException().
|
307 | 350 | *
|
@@ -412,6 +455,7 @@ module.exports = exports = {
|
412 | 455 | RangeError: makeNodeError(RangeError),
|
413 | 456 | URIError: makeNodeError(URIError),
|
414 | 457 | AssertionError,
|
| 458 | + codes, |
415 | 459 | E // This is exported only to facilitate testing.
|
416 | 460 | };
|
417 | 461 |
|
|
0 commit comments