Skip to content

Commit 88ae810

Browse files
committed
fix(utils::assertion): change to use named fallback error
1 parent 38f4798 commit 88ae810

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

docs/guides/error-warning-details.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,10 @@ This Error gets thrown when this package runs on an unsupported architecture by
7575
## BinaryNotFoundError
7676

7777
*extend documentation*
78+
79+
## AssertionFallbackError
80+
81+
Example: `Assert failed - no custom error`
82+
83+
Details:
84+
This Error gets thrown when no custom error to `assertion` is given, this should never happen

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { ChildProcess } from 'child_process';
33
import * as utils from '../utils';
44
import * as tmp from 'tmp';
55
import { resolve } from 'path';
6-
import { BinaryNotFoundError, InsufficientPermissionsError } from '../errors';
6+
import {
7+
AssertionFallbackError,
8+
BinaryNotFoundError,
9+
InsufficientPermissionsError,
10+
} from '../errors';
711
import { assertIsError } from '../../__tests__/testUtils/test_utils';
812

913
tmp.setGracefulCleanup();
@@ -47,13 +51,18 @@ describe('utils', () => {
4751
});
4852

4953
describe('assertion', () => {
54+
it('should run without error', () => {
55+
expect(utils.assertion(true)).toStrictEqual(void 0);
56+
});
57+
5058
it('should throw default error if none provided', () => {
51-
expect.assertions(1);
59+
expect.assertions(2);
5260
try {
5361
utils.assertion(false);
5462
fail('Expected Assertion to Throw');
5563
} catch (err) {
5664
assertIsError(err);
65+
expect(err).toBeInstanceOf(AssertionFallbackError);
5766
expect(err.message).toMatchSnapshot();
5867
}
5968
});

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,12 @@ export class BinaryNotFoundError extends Error {
115115
super(`No Binary at path "${path}" was found! (ENOENT)`);
116116
}
117117
}
118+
119+
/**
120+
* Custom Fallback Error for "utils.assertion", it is a named/custom Error to confuse less in the stacktrace
121+
*/
122+
export class AssertionFallbackError extends Error {
123+
constructor() {
124+
super('Assert failed - no custom error');
125+
}
126+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { ChildProcess } from 'child_process';
33
import { AutomaticAuth } from '../MongoMemoryServer';
44
import { promises as fspromises, Stats, constants } from 'fs';
55
import { LinuxOS } from './getos';
6-
import { BinaryNotFoundError, InsufficientPermissionsError } from './errors';
6+
import {
7+
AssertionFallbackError,
8+
BinaryNotFoundError,
9+
InsufficientPermissionsError,
10+
} from './errors';
711

812
const log = debug('MongoMS:utils');
913

@@ -76,7 +80,7 @@ export function isNullOrUndefined(val: unknown): val is null | undefined {
7680
*/
7781
export function assertion(cond: unknown, error?: Error): asserts cond {
7882
if (!cond) {
79-
throw error ?? new Error('Assert failed - no custom error');
83+
throw error ?? new AssertionFallbackError();
8084
}
8185
}
8286

0 commit comments

Comments
 (0)