Skip to content

Commit 4b57634

Browse files
committed
fix(MongoInstance): change that stderrHandler also runs error checks
this should fix that the actual error will be shown, even without debug enabled add function "checkErrorInLine", which is called from "stderrHandler" and "stdoutHandler" closes #632
1 parent 503feb1 commit 4b57634

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

packages/mongodb-memory-server-core/src/util/MongoInstance.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (/waiting for connections/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 (/transition to \w+ from \w+/i.test(line)) {
552+
const state = /transition to (\w+) from \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 (/transition to primary complete; database writes are now permitted/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 (/address already in use/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 (/transition to \w+ from \w+/i.test(line)) {
602-
const state = /transition to (\w+) from \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 (/transition to primary complete; database writes are now permitted/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

Comments
 (0)