@@ -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 )
0 commit comments