@@ -519,8 +519,11 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
519519 * @fires MongoInstance#instanceSTDERR
520520 */
521521 stderrHandler ( message : string | Buffer ) : void {
522- this . debug ( `stderrHandler: ""${ message . toString ( ) } ""` ) ; // denoting the STDERR string with double quotes, because the stdout might also use quotes
523- this . emit ( MongoInstanceEvents . instanceSTDERR , message ) ;
522+ const line : string = message . toString ( ) . trim ( ) ;
523+ this . debug ( `stderrHandler: ""${ line } ""` ) ; // denoting the STDERR string with double quotes, because the stdout might also use quotes
524+ this . emit ( MongoInstanceEvents . instanceSTDERR , line ) ;
525+
526+ this . checkErrorInLine ( line ) ;
524527 }
525528
526529 /**
@@ -541,6 +544,30 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
541544 if ( / w a i t i n g f o r c o n n e c t i o n s / i. test ( line ) ) {
542545 this . emit ( MongoInstanceEvents . instanceReady ) ;
543546 }
547+
548+ this . checkErrorInLine ( line ) ;
549+
550+ // this case needs to be infront of "transition to primary complete", otherwise it might reset "isInstancePrimary" to "false"
551+ if ( / t r a n s i t i o n t o \w + f r o m \w + / i. test ( line ) ) {
552+ const state = / t r a n s i t i o n t o ( \w + ) f r o m \w + / i. exec ( line ) ?. [ 1 ] ?? 'UNKNOWN' ;
553+ this . emit ( MongoInstanceEvents . instanceReplState , state ) ;
554+
555+ if ( state !== 'PRIMARY' ) {
556+ this . isInstancePrimary = false ;
557+ }
558+ }
559+ if ( / t r a n s i t i o n t o p r i m a r y c o m p l e t e ; d a t a b a s e w r i t e s a r e n o w p e r m i t t e d / i. test ( line ) ) {
560+ this . isInstancePrimary = true ;
561+ this . debug ( 'stdoutHandler: emitting "instancePrimary"' ) ;
562+ this . emit ( MongoInstanceEvents . instancePrimary ) ;
563+ }
564+ }
565+
566+ /**
567+ * Run Checks on the line if the lines contain any thrown errors
568+ * @param line The Line to check
569+ */
570+ protected checkErrorInLine ( line : string ) {
544571 if ( / a d d r e s s a l r e a d y i n u s e / i. test ( line ) ) {
545572 this . emit (
546573 MongoInstanceEvents . instanceError ,
@@ -597,20 +624,6 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
597624 new StdoutInstanceError ( 'Mongod internal error' )
598625 ) ;
599626 }
600- // this case needs to be infront of "transition to primary complete", otherwise it might reset "isInstancePrimary" to "false"
601- if ( / t r a n s i t i o n t o \w + f r o m \w + / i. test ( line ) ) {
602- const state = / t r a n s i t i o n t o ( \w + ) f r o m \w + / i. exec ( line ) ?. [ 1 ] ?? 'UNKNOWN' ;
603- this . emit ( MongoInstanceEvents . instanceReplState , state ) ;
604-
605- if ( state !== 'PRIMARY' ) {
606- this . isInstancePrimary = false ;
607- }
608- }
609- if ( / t r a n s i t i o n t o p r i m a r y c o m p l e t e ; d a t a b a s e w r i t e s a r e n o w p e r m i t t e d / i. test ( line ) ) {
610- this . isInstancePrimary = true ;
611- this . debug ( 'stdoutHandler: emitting "instancePrimary"' ) ;
612- this . emit ( MongoInstanceEvents . instancePrimary ) ;
613- }
614627 }
615628}
616629
0 commit comments