Skip to content

Commit 2131df2

Browse files
committed
fix(MongoInstance::checkErrorInLine): hange json output of error "DBException in initAndListen"
1 parent 0f80c2e commit 2131df2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,21 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
591591
)
592592
);
593593
}
594+
595+
// special handling for when mongodb outputs this error as json
596+
const execptionMatchJson = /\bDBException in initAndListen,/i.test(line);
597+
598+
if (execptionMatchJson) {
599+
const loadedJSON = JSON.parse(line) ?? {};
600+
601+
this.emit(
602+
MongoInstanceEvents.instanceError,
603+
new StdoutInstanceError(
604+
`Instance Failed to start with "DBException in initAndListen". Original Error:\n` +
605+
loadedJSON?.attr?.error ?? line // try to use the parsed json, but as fallback use the entire line
606+
)
607+
);
608+
}
594609
}
595610

596611
if (/CURL_OPENSSL_3['\s]+not found/i.test(line)) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,24 @@ describe('MongodbInstance', () => {
485485
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
486486
expect(event.message).toMatchSnapshot();
487487
});
488+
489+
it('DBException in initAndListen', () => {
490+
// actual line copied from mongod 6.1.0
491+
// can be reproduced with "mongodb --storageEngine ephemeralForTest"
492+
const line =
493+
'{"t":{"$date":"2022-09-01T08:54:54.598+00:00"},"s":"E", "c":"CONTROL", "id":20557, "ctx":"initandlisten","msg":"DBException in initAndListen, terminating","attr":{"error":"Location18656: Cannot start server with an unknown storage engine: ephemeralForTest"}}';
494+
495+
mongod.stdoutHandler(line);
496+
497+
console.log(events);
498+
expect(events.size).toEqual(2);
499+
expect(events.get(MongoInstanceEvents.instanceSTDOUT)).toEqual(line);
500+
501+
const event = events.get(MongoInstanceEvents.instanceError);
502+
expect(event).toBeInstanceOf(StdoutInstanceError);
503+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
504+
expect(event.message).toMatchSnapshot();
505+
});
488506
});
489507
});
490508

packages/mongodb-memory-server-core/src/util/__tests__/__snapshots__/MongoInstance.test.ts.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ exports[`MongodbInstance prepareCommandArgs should throw an error if no port is
88

99
exports[`MongodbInstance test events checkErrorInLine() should emit "instanceError" when shared libraries fail to load 1`] = `"Instance failed to start because a library is missing or cannot be opened: \\"libcrypto.so.1.1\\""`;
1010

11+
exports[`MongodbInstance test events stdoutHandler() should emit "instanceError" when "excepetion in initAndListen" is thrown DBException in initAndListen 1`] = `
12+
"Instance Failed to start with \\"DBException in initAndListen\\". Original Error:
13+
Location18656: Cannot start server with an unknown storage engine: ephemeralForTest"
14+
`;
15+
1116
exports[`MongodbInstance test events stdoutHandler() should emit "instanceError" when "excepetion in initAndListen" is thrown DbPathInUse (Not a directory) 1`] = `
1217
"Instance Failed to start with \\"DBPathInUse\\". Original Error:
1318
Unable to create/open the lock file: /dev/null/mongod.lock (Not a directory). Ensure the user executing mongod is the owner of the lock file and has the appropriate permissions. Also make sure that another mongod instance is not already running on the /dev/null directory"

0 commit comments

Comments
 (0)