@@ -41,6 +41,13 @@ EventEmitter.prototype._maxListeners = undefined;
4141// added to it. This is a useful default which helps finding memory leaks.
4242var defaultMaxListeners = 10 ;
4343
44+ var errors ;
45+ function lazyErrors ( ) {
46+ if ( errors === undefined )
47+ errors = require ( 'internal/errors' ) ;
48+ return errors ;
49+ }
50+
4451Object . defineProperty ( EventEmitter , 'defaultMaxListeners' , {
4552 enumerable : true ,
4653 get : function ( ) {
@@ -52,8 +59,10 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
5259 console ;
5360 // check whether the input is a positive number (whose value is zero or
5461 // 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+ }
5766 defaultMaxListeners = arg ;
5867 }
5968} ) ;
@@ -79,8 +88,10 @@ EventEmitter.init = function() {
7988// Obviously not all Emitters should be limited to 10. This function allows
8089// that to be increased. Set to zero for unlimited.
8190EventEmitter . 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+ }
8495 this . _maxListeners = n ;
8596 return this ;
8697} ;
@@ -170,8 +181,10 @@ EventEmitter.prototype.emit = function emit(type) {
170181 if ( arguments . length > 1 )
171182 er = arguments [ 1 ] ;
172183 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+ }
175188 if ( typeof er === 'object' && er !== null ) {
176189 er . domainEmitter = this ;
177190 er . domain = domain ;
@@ -182,7 +195,8 @@ EventEmitter.prototype.emit = function emit(type) {
182195 throw er ; // Unhandled 'error' event
183196 } else {
184197 // 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 ) ;
186200 err . context = er ;
187201 throw err ;
188202 }
@@ -234,8 +248,10 @@ function _addListener(target, type, listener, prepend) {
234248 var events ;
235249 var existing ;
236250
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+ }
239255
240256 events = target . _events ;
241257 if ( ! events ) {
@@ -278,6 +294,7 @@ function _addListener(target, type, listener, prepend) {
278294 m = $getMaxListeners ( target ) ;
279295 if ( m && m > 0 && existing . length > m ) {
280296 existing . warned = true ;
297+ // No error code for this since it is a Warning
281298 const w = new Error ( 'Possible EventEmitter memory leak detected. ' +
282299 `${ existing . length } ${ String ( type ) } listeners ` +
283300 'added. Use emitter.setMaxListeners() to ' +
@@ -337,16 +354,21 @@ function _onceWrap(target, type, listener) {
337354}
338355
339356EventEmitter . 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+ }
342361 this . on ( type , _onceWrap ( this , type , listener ) ) ;
343362 return this ;
344363} ;
345364
346365EventEmitter . prototype . prependOnceListener =
347366 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+ }
350372 this . prependListener ( type , _onceWrap ( this , type , listener ) ) ;
351373 return this ;
352374 } ;
@@ -356,8 +378,11 @@ EventEmitter.prototype.removeListener =
356378 function removeListener ( type , listener ) {
357379 var list , events , position , i , originalListener ;
358380
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+ }
361386
362387 events = this . _events ;
363388 if ( ! events )
0 commit comments