Skip to content

Commit

Permalink
feat: merge "EnsureInstanceError" into "InstanceInfoError"
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed May 8, 2023
1 parent 4bb3420 commit 6962cbe
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
4 changes: 4 additions & 0 deletions docs/guides/error-warning-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ Default Timeout: `1000 * 30` (30 seconds)

## EnsureInstanceError

:::note
Merged into [`InstanceInfoError`](#instanceinfoerror) in 9.0.0
:::

Example:

- `${baseMesasge} state was "running" but "instanceInfo" was undefined!`
Expand Down
11 changes: 6 additions & 5 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import debug from 'debug';
import { EventEmitter } from 'events';
import { promises as fspromises } from 'fs';
import { AddUserOptions, MongoClient } from 'mongodb';
import { EnsureInstanceError, StateError } from './util/errors';
import { InstanceInfoError, StateError } from './util/errors';
import * as os from 'os';

const log = debug('MongoMS:MongoMemoryServer');
Expand Down Expand Up @@ -627,7 +627,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
return this._instanceInfo;
}

throw new EnsureInstanceError(true);
throw new InstanceInfoError('MongoMemoryServer.ensureInstance (state: running)');
case MongoMemoryServerStates.new:
case MongoMemoryServerStates.stopped:
break;
Expand Down Expand Up @@ -670,9 +670,10 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
this.debug('ensureInstance: "start()" command was succesfully resolved');

// check again for 1. Typescript-type reasons and 2. if .start failed to throw an error
if (!this._instanceInfo) {
throw new EnsureInstanceError(false);
}
assertion(
!!this._instanceInfo,
new InstanceInfoError('MongoMemoryServer.ensureInstance (after starting)')
);

return this._instanceInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import MongoMemoryServer, {
} from '../MongoMemoryServer';
import MongoInstance from '../util/MongoInstance';
import * as utils from '../util/utils';
import { EnsureInstanceError, StateError } from '../util/errors';
import { InstanceInfoError, StateError } from '../util/errors';
import { assertIsError } from './testUtils/test_utils';
import { promises as fspromises } from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -523,8 +523,9 @@ describe('MongoMemoryServer', () => {
await mongoServer.ensureInstance();
fail('Expected "ensureInstance" to fail');
} catch (err) {
expect(err).toBeInstanceOf(EnsureInstanceError);
expect(JSON.stringify(err)).toMatchSnapshot(); // this is to test all the custom values on the error
expect(err).toBeInstanceOf(InstanceInfoError);
assertIsError(err);
expect(err.message).toMatchSnapshot();
}

expect(mongoServer.start).toHaveBeenCalledTimes(1);
Expand All @@ -549,8 +550,9 @@ describe('MongoMemoryServer', () => {
await mongoServer.ensureInstance();
fail('Expected "ensureInstance" to fail');
} catch (err) {
expect(err).toBeInstanceOf(EnsureInstanceError);
expect(JSON.stringify(err)).toMatchSnapshot(); // this is to test all the custom values on the error
expect(err).toBeInstanceOf(InstanceInfoError);
assertIsError(err);
expect(err.message).toMatchSnapshot();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ This may be because of using a v6.x way of calling functions, look at the follow
https://nodkz.github.io/mongodb-memory-server/docs/guides/migration/migrate7#no-function-other-than-start-create-ensureinstance-will-be-starting-anything"
`;

exports[`MongoMemoryServer ensureInstance() should throw an error if "instanceInfo" is undefined but "_state" is "running" 1`] = `"{\\"isRunning\\":true,\\"message\\":\\"\\\\\\"ensureInstance\\\\\\" failed, because state was \\\\\\"running\\\\\\" but \\\\\\"instanceInfo\\\\\\" was undefined!\\"}"`;
exports[`MongoMemoryServer ensureInstance() should throw an error if "instanceInfo" is undefined but "_state" is "running" 1`] = `"\\"instanceInfo\\" was undefined when expected to be defined! (where: \\"MongoMemoryServer.ensureInstance (state: running)\\")"`;

exports[`MongoMemoryServer ensureInstance() should throw an error if no "instanceInfo" is defined after calling start 1`] = `"{\\"isRunning\\":false,\\"message\\":\\"\\\\\\"ensureInstance\\\\\\" failed, because \\\\\\"instanceInfo\\\\\\" was undefined after running \\\\\\"start\\\\\\"\\"}"`;
exports[`MongoMemoryServer ensureInstance() should throw an error if no "instanceInfo" is defined after calling start 1`] = `"\\"instanceInfo\\" was undefined when expected to be defined! (where: \\"MongoMemoryServer.ensureInstance (after starting)\\")"`;

exports[`MongoMemoryServer ensureInstance() should throw an error if the given "_state" has no case 1`] = `
"Incorrect State for operation: \\"not Existing\\", allowed States: \\"[running,new,stopped,starting]\\"
Expand Down
14 changes: 0 additions & 14 deletions packages/mongodb-memory-server-core/src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,6 @@ export class WaitForPrimaryTimeoutError extends Error {
}
}

// REFACTOR: consider merging this with InstanceInfoError
export class EnsureInstanceError extends Error {
constructor(public isRunning: boolean) {
super();
const baseMesasge = '"ensureInstance" failed, because';

if (isRunning) {
this.message = `${baseMesasge} state was "running" but "instanceInfo" was undefined!`;
} else {
this.message = `${baseMesasge} "instanceInfo" was undefined after running "start"`;
}
}
}

// REFACTOR: merge this error with BinaryNotFoundError
export class NoSystemBinaryFoundError extends Error {
constructor(public binaryPath: string) {
Expand Down

0 comments on commit 6962cbe

Please sign in to comment.