Skip to content

Commit

Permalink
fix(MongoInstance::checkErrorInLine): hange json output of error "DBE…
Browse files Browse the repository at this point in the history
…xception in initAndListen"
  • Loading branch information
hasezoey committed Sep 1, 2022
1 parent 0f80c2e commit 2131df2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/mongodb-memory-server-core/src/util/MongoInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,21 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
)
);
}

// special handling for when mongodb outputs this error as json
const execptionMatchJson = /\bDBException in initAndListen,/i.test(line);

if (execptionMatchJson) {
const loadedJSON = JSON.parse(line) ?? {};

this.emit(
MongoInstanceEvents.instanceError,
new StdoutInstanceError(
`Instance Failed to start with "DBException in initAndListen". Original Error:\n` +
loadedJSON?.attr?.error ?? line // try to use the parsed json, but as fallback use the entire line
)
);
}
}

if (/CURL_OPENSSL_3['\s]+not found/i.test(line)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,24 @@ describe('MongodbInstance', () => {
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(event.message).toMatchSnapshot();
});

it('DBException in initAndListen', () => {
// actual line copied from mongod 6.1.0
// can be reproduced with "mongodb --storageEngine ephemeralForTest"
const line =
'{"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"}}';

mongod.stdoutHandler(line);

console.log(events);
expect(events.size).toEqual(2);
expect(events.get(MongoInstanceEvents.instanceSTDOUT)).toEqual(line);

const event = events.get(MongoInstanceEvents.instanceError);
expect(event).toBeInstanceOf(StdoutInstanceError);
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
expect(event.message).toMatchSnapshot();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ exports[`MongodbInstance prepareCommandArgs should throw an error if no port is

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\\""`;

exports[`MongodbInstance test events stdoutHandler() should emit "instanceError" when "excepetion in initAndListen" is thrown DBException in initAndListen 1`] = `
"Instance Failed to start with \\"DBException in initAndListen\\". Original Error:
Location18656: Cannot start server with an unknown storage engine: ephemeralForTest"
`;

exports[`MongodbInstance test events stdoutHandler() should emit "instanceError" when "excepetion in initAndListen" is thrown DbPathInUse (Not a directory) 1`] = `
"Instance Failed to start with \\"DBPathInUse\\". Original Error:
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"
Expand Down

0 comments on commit 2131df2

Please sign in to comment.