Skip to content

Commit 83626e0

Browse files
committed
feat(errors): add "StartBinaryFailedError"
1 parent 6ca70f0 commit 83626e0

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { lt } from 'semver';
77
import { EventEmitter } from 'events';
88
import { MongoClient, MongoNetworkError } from 'mongodb';
99
import { promises as fspromises, constants } from 'fs';
10+
import { StartBinaryFailedError } from './errors';
1011

1112
if (lt(process.version, '10.15.0')) {
1213
console.warn('Using NodeJS below 10.15.0');
@@ -391,7 +392,7 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
391392
childProcess.on('error', this.errorHandler.bind(this));
392393

393394
if (isNullOrUndefined(childProcess.pid)) {
394-
throw new Error('Spawned Mongo Instance PID is undefined');
395+
throw new StartBinaryFailedError(path.resolve(mongoBin));
395396
}
396397

397398
this.emit(MongoInstanceEvents.instanceLaunched);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as dbUtil from '../utils';
44
import MongodbInstance, { MongoInstanceEvents } from '../MongoInstance';
55
import resolveConfig, { ResolveConfigVariables } from '../resolveConfig';
66
import getPort from 'get-port';
7+
import { StartBinaryFailedError } from '../errors';
78

89
jest.setTimeout(100000); // 10s
910
tmp.setGracefulCleanup();
@@ -216,12 +217,14 @@ describe('MongodbInstance', () => {
216217

217218
it('"_launchMongod" should throw an error if "mongodProcess.pid" is undefined', () => {
218219
const mongod = new MongodbInstance({ instance: { port: 0, dbPath: '' } }); // dummy values - they shouldnt matter
220+
const mockBinary = 'thisShouldNotExist';
219221

220222
try {
221-
mongod._launchMongod('thisShouldNotExist');
223+
mongod._launchMongod(mockBinary);
222224
fail('Expected "_launchMongod" to throw');
223225
} catch (err) {
224-
expect(err.message).toEqual('Spawned Mongo Instance PID is undefined');
226+
expect(err).toBeInstanceOf(StartBinaryFailedError);
227+
expect(JSON.stringify(err)).toMatchSnapshot(); // this is to test all the custom values on the error
225228
}
226229
});
227230

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`MongodbInstance "_launchMongod" should throw an error if "mongodProcess.pid" is undefined 1`] = `"{\\"binary\\":\\"/projects/nodejs/mongodb-memory-server/packages/mongodb-memory-server-core/thisShouldNotExist\\"}"`;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@ export class Md5CheckFailedError extends Error {
7878
super(`MD5 check failed! Binary MD5 is "${binarymd5}", Checkfile MD5 is "${checkfilemd5}"`);
7979
}
8080
}
81+
82+
export class StartBinaryFailedError extends Error {
83+
constructor(public binary: string) {
84+
super(`Starting the Binary Failed (PID is undefined)! Binary: "${binary}"`);
85+
}
86+
}

0 commit comments

Comments
 (0)