From 6962cbec68c7098f491b4c7c6f66b240e03a6659 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Mon, 8 May 2023 16:07:13 +0200 Subject: [PATCH] feat: merge "EnsureInstanceError" into "InstanceInfoError" --- docs/guides/error-warning-details.md | 4 ++++ .../src/MongoMemoryServer.ts | 11 ++++++----- .../src/__tests__/MongoMemoryServer.test.ts | 12 +++++++----- .../__snapshots__/MongoMemoryServer.test.ts.snap | 4 ++-- .../mongodb-memory-server-core/src/util/errors.ts | 14 -------------- 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/docs/guides/error-warning-details.md b/docs/guides/error-warning-details.md index 466265e60..eb59e20fb 100644 --- a/docs/guides/error-warning-details.md +++ b/docs/guides/error-warning-details.md @@ -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!` diff --git a/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts b/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts index 66104bb3c..4060069c3 100644 --- a/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts +++ b/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts @@ -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'); @@ -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; @@ -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; } diff --git a/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts b/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts index f0f152bf5..d221025ad 100644 --- a/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts +++ b/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts @@ -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'; @@ -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); @@ -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(); } }); diff --git a/packages/mongodb-memory-server-core/src/__tests__/__snapshots__/MongoMemoryServer.test.ts.snap b/packages/mongodb-memory-server-core/src/__tests__/__snapshots__/MongoMemoryServer.test.ts.snap index 11dde9a85..d51cc1ed2 100644 --- a/packages/mongodb-memory-server-core/src/__tests__/__snapshots__/MongoMemoryServer.test.ts.snap +++ b/packages/mongodb-memory-server-core/src/__tests__/__snapshots__/MongoMemoryServer.test.ts.snap @@ -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]\\" diff --git a/packages/mongodb-memory-server-core/src/util/errors.ts b/packages/mongodb-memory-server-core/src/util/errors.ts index 3eccb9efe..08c8b91f8 100644 --- a/packages/mongodb-memory-server-core/src/util/errors.ts +++ b/packages/mongodb-memory-server-core/src/util/errors.ts @@ -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) {