| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a26c1bf commit e5ad545
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1251,6 +1251,12 @@ buffer. | |||
| 1251 | 1251 | ||
| 1252 | 1252 | Used when a string that contains unescaped characters was received. | |
| 1253 | 1253 | ||
| 1254 | + <a id="ERR_UNHANDLED_ERROR"></a> | ||
| 1255 | + ### ERR_UNHANDLED_ERROR | ||
| 1256 | + | ||
| 1257 | + Used when an unhandled "error" occurs (for instance, when an `'error'` event | ||
| 1258 | + is emitted by an `EventEmitter` but an `'error'` handler is not registered). | ||
| 1259 | + | ||
| 1254 | 1260 | <a id="ERR_UNKNOWN_ENCODING"></a> | |
| 1255 | 1261 | ### ERR_UNKNOWN_ENCODING | |
| 1256 | 1262 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,13 @@ EventEmitter.prototype._maxListeners = undefined; | |||
| 41 | 41 | // added to it. This is a useful default which helps finding memory leaks. | |
| 42 | 42 | var defaultMaxListeners = 10; | |
| 43 | 43 | ||
| 44 | + var errors; | ||
| 45 | + function lazyErrors() { | ||
| 46 | + if (errors === undefined) | ||
| 47 | + errors = require('internal/errors'); | ||
| 48 | + return errors; | ||
| 49 | + } | ||
| 50 | + | ||
| 44 | 51 | Object.defineProperty(EventEmitter, 'defaultMaxListeners', { | |
| 45 | 52 | enumerable: true, | |
| 46 | 53 | get: function() { | |
@@ -52,8 +59,10 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', { | |||
| 52 | 59 | console; | |
| 53 | 60 | // check whether the input is a positive number (whose value is zero or | |
| 54 | 61 | // greater and not a NaN). | |
| 55 | - if (typeof arg !== 'number' || arg < 0 || arg !== arg) | ||
| 56 | - throw new TypeError('"defaultMaxListeners" must be a positive number'); | ||
| 62 | + if (typeof arg !== 'number' || arg < 0 || arg !== arg) { | ||
| 63 | + const errors = lazyErrors(); | ||
| 64 | + throw new errors.TypeError('ERR_OUT_OF_RANGE', 'defaultMaxListeners'); | ||
| 65 | + } | ||
| 57 | 66 | defaultMaxListeners = arg; | |
| 58 | 67 | } | |
| 59 | 68 | }); | |
@@ -79,8 +88,10 @@ EventEmitter.init = function() { | |||
| 79 | 88 | // Obviously not all Emitters should be limited to 10. This function allows | |
| 80 | 89 | // that to be increased. Set to zero for unlimited. | |
| 81 | 90 | EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { | |
| 82 | - if (typeof n !== 'number' || n < 0 || isNaN(n)) | ||
| 83 | - throw new TypeError('"n" argument must be a positive number'); | ||
| 91 | + if (typeof n !== 'number' || n < 0 || isNaN(n)) { | ||
| 92 | + const errors = lazyErrors(); | ||
| 93 | + throw new errors.TypeError('ERR_OUT_OF_RANGE', 'n'); | ||
| 94 | + } | ||
| 84 | 95 | this._maxListeners = n; | |
| 85 | 96 | return this; | |
| 86 | 97 | }; | |
@@ -170,8 +181,10 @@ EventEmitter.prototype.emit = function emit(type) { | |||
| 170 | 181 | if (arguments.length > 1) | |
| 171 | 182 | er = arguments[1]; | |
| 172 | 183 | if (domain) { | |
| 173 | - if (!er) | ||
| 174 | - er = new Error('Unhandled "error" event'); | ||
| 184 | + if (!er) { | ||
| 185 | + const errors = lazyErrors(); | ||
| 186 | + er = new errors.Error('ERR_UNHANDLED_ERROR'); | ||
| 187 | + } | ||
| 175 | 188 | if (typeof er === 'object' && er !== null) { | |
| 176 | 189 | er.domainEmitter = this; | |
| 177 | 190 | er.domain = domain; | |
@@ -182,7 +195,8 @@ EventEmitter.prototype.emit = function emit(type) { | |||
| 182 | 195 | throw er; // Unhandled 'error' event | |
| 183 | 196 | } else { | |
| 184 | 197 | // At least give some kind of context to the user | |
| 185 | - const err = new Error('Unhandled "error" event. (' + er + ')'); | ||
| 198 | + const errors = lazyErrors(); | ||
| 199 | + const err = new errors.Error('ERR_UNHANDLED_ERROR', er); | ||
| 186 | 200 | err.context = er; | |
| 187 | 201 | throw err; | |
| 188 | 202 | } | |
@@ -234,8 +248,10 @@ function _addListener(target, type, listener, prepend) { | |||
| 234 | 248 | var events; | |
| 235 | 249 | var existing; | |
| 236 | 250 | ||
| 237 | - if (typeof listener !== 'function') | ||
| 238 | - throw new TypeError('"listener" argument must be a function'); | ||
| 251 | + if (typeof listener !== 'function') { | ||
| 252 | + const errors = lazyErrors(); | ||
| 253 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'function'); | ||
| 254 | + } | ||
| 239 | 255 | ||
| 240 | 256 | events = target._events; | |
| 241 | 257 | if (!events) { | |
@@ -278,6 +294,7 @@ function _addListener(target, type, listener, prepend) { | |||
| 278 | 294 | m = $getMaxListeners(target); | |
| 279 | 295 | if (m && m > 0 && existing.length > m) { | |
| 280 | 296 | existing.warned = true; | |
| 297 | + // No error code for this since it is a Warning | ||
| 281 | 298 | const w = new Error('Possible EventEmitter memory leak detected. ' + | |
| 282 | 299 | `${existing.length} ${String(type)} listeners ` + | |
| 283 | 300 | 'added. Use emitter.setMaxListeners() to ' + | |
@@ -337,16 +354,21 @@ function _onceWrap(target, type, listener) { | |||
| 337 | 354 | } | |
| 338 | 355 | ||
| 339 | 356 | EventEmitter.prototype.once = function once(type, listener) { | |
| 340 | - if (typeof listener !== 'function') | ||
| 341 | - throw new TypeError('"listener" argument must be a function'); | ||
| 357 | + if (typeof listener !== 'function') { | ||
| 358 | + const errors = lazyErrors(); | ||
| 359 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'function'); | ||
| 360 | + } | ||
| 342 | 361 | this.on(type, _onceWrap(this, type, listener)); | |
| 343 | 362 | return this; | |
| 344 | 363 | }; | |
| 345 | 364 | ||
| 346 | 365 | EventEmitter.prototype.prependOnceListener = | |
| 347 | 366 | function prependOnceListener(type, listener) { | |
| 348 | - if (typeof listener !== 'function') | ||
| 349 | - throw new TypeError('"listener" argument must be a function'); | ||
| 367 | + if (typeof listener !== 'function') { | ||
| 368 | + const errors = lazyErrors(); | ||
| 369 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', | ||
| 370 | + 'function'); | ||
| 371 | + } | ||
| 350 | 372 | this.prependListener(type, _onceWrap(this, type, listener)); | |
| 351 | 373 | return this; | |
| 352 | 374 | }; | |
@@ -356,8 +378,11 @@ EventEmitter.prototype.removeListener = | |||
| 356 | 378 | function removeListener(type, listener) { | |
| 357 | 379 | var list, events, position, i, originalListener; | |
| 358 | 380 | ||
| 359 | - if (typeof listener !== 'function') | ||
| 360 | - throw new TypeError('"listener" argument must be a function'); | ||
| 381 | + if (typeof listener !== 'function') { | ||
| 382 | + const errors = lazyErrors(); | ||
| 383 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', | ||
| 384 | + 'function'); | ||
| 385 | + } | ||
| 361 | 386 | ||
| 362 | 387 | events = this._events; | |
| 363 | 388 | if (!events) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -327,6 +327,12 @@ E('ERR_TRANSFORM_WITH_LENGTH_0', | |||
| 327 | 327 | 'Calling transform done when writableState.length != 0'); | |
| 328 | 328 | E('ERR_UNESCAPED_CHARACTERS', | |
| 329 | 329 | (name) => `${name} contains unescaped characters`); | |
| 330 | + E('ERR_UNHANDLED_ERROR', | ||
| 331 | + (err) => { | ||
| 332 | + const msg = 'Unhandled error.'; | ||
| 333 | + if (err === undefined) return msg; | ||
| 334 | + return `${msg} (${err})`; | ||
| 335 | + }); | ||
| 330 | 336 | E('ERR_UNKNOWN_ENCODING', 'Unknown encoding: %s'); | |
| 331 | 337 | E('ERR_UNKNOWN_FILE_EXTENSION', 'Unknown file extension: %s'); | |
| 332 | 338 | E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,8 +86,11 @@ const EventEmitter = require('events'); | |||
| 86 | 86 | } | |
| 87 | 87 | ||
| 88 | 88 | // Verify that the listener must be a function | |
| 89 | - assert.throws(() => { | ||
| 89 | + common.expectsError(() => { | ||
| 90 | 90 | const ee = new EventEmitter(); | |
| 91 | - | ||
| 92 | 91 | ee.on('foo', null); | |
| 93 | - }, /^TypeError: "listener" argument must be a function$/); | ||
| 92 | + }, { | ||
| 93 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 94 | + type: TypeError, | ||
| 95 | + message: 'The "listener" argument must be of type function' | ||
| 96 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,23 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | - require('../common'); | ||
| 2 | + const common = require('../common'); | ||
| 3 | 3 | const EventEmitter = require('events'); | |
| 4 | - const assert = require('assert'); | ||
| 5 | 4 | ||
| 6 | 5 | const EE = new EventEmitter(); | |
| 7 | 6 | ||
| 8 | - assert.throws(() => { | ||
| 9 | - EE.emit('error', 'Accepts a string'); | ||
| 10 | - }, /^Error: Unhandled "error" event\. \(Accepts a string\)$/); | ||
| 7 | + common.expectsError( | ||
| 8 | + () => EE.emit('error', 'Accepts a string'), | ||
| 9 | + { | ||
| 10 | + code: 'ERR_UNHANDLED_ERROR', | ||
| 11 | + type: Error, | ||
| 12 | + message: 'Unhandled error. (Accepts a string)' | ||
| 13 | + } | ||
| 14 | + ); | ||
| 11 | 15 | ||
| 12 | - assert.throws(() => { | ||
| 13 | - EE.emit('error', { message: 'Error!' }); | ||
| 14 | - }, /^Error: Unhandled "error" event\. \(\[object Object\]\)$/); | ||
| 16 | + common.expectsError( | ||
| 17 | + () => EE.emit('error', { message: 'Error!' }), | ||
| 18 | + { | ||
| 19 | + code: 'ERR_UNHANDLED_ERROR', | ||
| 20 | + type: Error, | ||
| 21 | + message: 'Unhandled error. ([object Object])' | ||
| 22 | + } | ||
| 23 | + ); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,6 @@ | |||
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | 23 | const common = require('../common'); | |
| 24 | - const assert = require('assert'); | ||
| 25 | 24 | const events = require('events'); | |
| 26 | 25 | const e = new events.EventEmitter(); | |
| 27 | 26 | ||
@@ -31,12 +30,25 @@ e.on('maxListeners', common.mustCall()); | |||
| 31 | 30 | e.setMaxListeners(42); | |
| 32 | 31 | ||
| 33 | 32 | const throwsObjs = [NaN, -1, 'and even this']; | |
| 34 | - const maxError = /^TypeError: "n" argument must be a positive number$/; | ||
| 35 | - const defError = /^TypeError: "defaultMaxListeners" must be a positive number$/; | ||
| 36 | 33 | ||
| 37 | 34 | for (const obj of throwsObjs) { | |
| 38 | - assert.throws(() => e.setMaxListeners(obj), maxError); | ||
| 39 | - assert.throws(() => events.defaultMaxListeners = obj, defError); | ||
| 35 | + common.expectsError( | ||
| 36 | + () => e.setMaxListeners(obj), | ||
| 37 | + { | ||
| 38 | + code: 'ERR_OUT_OF_RANGE', | ||
| 39 | + type: TypeError, | ||
| 40 | + message: 'The "n" argument is out of range' | ||
| 41 | + } | ||
| 42 | + ); | ||
| 43 | + | ||
| 44 | + common.expectsError( | ||
| 45 | + () => events.defaultMaxListeners = obj, | ||
| 46 | + { | ||
| 47 | + code: 'ERR_OUT_OF_RANGE', | ||
| 48 | + type: TypeError, | ||
| 49 | + message: 'The "defaultMaxListeners" argument is out of range' | ||
| 50 | + } | ||
| 51 | + ); | ||
| 40 | 52 | } | |
| 41 | 53 | ||
| 42 | 54 | e.emit('maxListeners'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,11 +50,15 @@ e.once('e', common.mustCall()); | |||
| 50 | 50 | e.emit('e'); | |
| 51 | 51 | ||
| 52 | 52 | // Verify that the listener must be a function | |
| 53 | - assert.throws(() => { | ||
| 53 | + common.expectsError(() => { | ||
| 54 | 54 | const ee = new EventEmitter(); | |
| 55 | 55 | ||
| 56 | 56 | ee.once('foo', null); | |
| 57 | - }, /^TypeError: "listener" argument must be a function$/); | ||
| 57 | + }, { | ||
| 58 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 59 | + type: TypeError, | ||
| 60 | + message: 'The "listener" argument must be of type function' | ||
| 61 | + }); | ||
| 58 | 62 | ||
| 59 | 63 | { | |
| 60 | 64 | // once() has different code paths based on the number of arguments being | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,11 +19,15 @@ myEE.prependOnceListener('foo', | |||
| 19 | 19 | myEE.emit('foo'); | |
| 20 | 20 | ||
| 21 | 21 | // Verify that the listener must be a function | |
| 22 | - assert.throws(() => { | ||
| 22 | + common.expectsError(() => { | ||
| 23 | 23 | const ee = new EventEmitter(); | |
| 24 | 24 | ||
| 25 | 25 | ee.prependOnceListener('foo', null); | |
| 26 | - }, /^TypeError: "listener" argument must be a function$/); | ||
| 26 | + }, { | ||
| 27 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 28 | + type: TypeError, | ||
| 29 | + message: 'The "listener" argument must be of type function' | ||
| 30 | + }); | ||
| 27 | 31 | ||
| 28 | 32 | // Test fallback if prependListener is undefined. | |
| 29 | 33 | const stream = require('stream'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -144,11 +144,15 @@ function listener2() {} | |||
| 144 | 144 | } | |
| 145 | 145 | ||
| 146 | 146 | // Verify that the removed listener must be a function | |
| 147 | - assert.throws(() => { | ||
| 147 | + common.expectsError(() => { | ||
| 148 | 148 | const ee = new EventEmitter(); | |
| 149 | 149 | ||
| 150 | 150 | ee.removeListener('foo', null); | |
| 151 | - }, /^TypeError: "listener" argument must be a function$/); | ||
| 151 | + }, { | ||
| 152 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 153 | + type: TypeError, | ||
| 154 | + message: 'The "listener" argument must be of type function' | ||
| 155 | + }); | ||
| 152 | 156 | ||
| 153 | 157 | { | |
| 154 | 158 | const ee = new EventEmitter(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments