Skip to content

Commit ff229fa

Browse files
feat(NODE-6065)!: throw MongoRuntimeError instead of MissingDependencyError in crypto connection (#4711)
1 parent dab4c7c commit ff229fa

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/cmap/connection.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
import {
2323
MongoCompatibilityError,
2424
MONGODB_ERROR_CODES,
25-
MongoMissingDependencyError,
2625
MongoNetworkError,
2726
MongoNetworkTimeoutError,
2827
MongoOperationTimeoutError,
@@ -868,11 +867,7 @@ export class CryptoConnection extends Connection {
868867
): Promise<Document> {
869868
const { autoEncrypter } = this;
870869
if (!autoEncrypter) {
871-
// TODO(NODE-6065): throw a MongoRuntimeError in Node V7
872-
// @ts-expect-error No cause provided because there is no underlying error.
873-
throw new MongoMissingDependencyError('No AutoEncrypter available for encryption', {
874-
dependencyName: 'n/a'
875-
});
870+
throw new MongoRuntimeError('No AutoEncrypter available for encryption');
876871
}
877872

878873
const serverWireVersion = maxWireVersion(this);

test/unit/cmap/connection.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ import * as sinon from 'sinon';
66
import { setTimeout } from 'timers/promises';
77

88
import { connect } from '../../../src/cmap/connect';
9-
import { Connection, SizedMessageTransform } from '../../../src/cmap/connection';
9+
import { Connection, CryptoConnection, SizedMessageTransform } from '../../../src/cmap/connection';
1010
import { MongoNetworkTimeoutError, MongoRuntimeError } from '../../../src/error';
1111
import { MongoClientAuthProviders } from '../../../src/mongo_client_auth_providers';
12-
import { isHello, MongoDBCollectionNamespace, ns, promiseWithResolvers } from '../../../src/utils';
12+
import {
13+
HostAddress,
14+
isHello,
15+
MongoDBCollectionNamespace,
16+
MongoDBNamespace,
17+
ns,
18+
promiseWithResolvers
19+
} from '../../../src/utils';
1320
import * as mock from '../../tools/mongodb-mock/index';
1421

1522
const connectionOptionsDefaults = {
@@ -402,3 +409,22 @@ describe('new Connection()', function () {
402409
});
403410
});
404411
});
412+
413+
describe('class CryptoConnection {}', function () {
414+
it('CryptoConnection.command() throws if no autoEncrypter is configured', async function () {
415+
const connection = new CryptoConnection(new Socket(), {
416+
...connectionOptionsDefaults,
417+
hostAddress: HostAddress.fromString('localhost:27017'),
418+
authProviders: new MongoClientAuthProviders(),
419+
extendedMetadata: Promise.resolve({})
420+
});
421+
422+
const error = await connection
423+
.command(MongoDBNamespace.fromString('foo.bar'), {}, {})
424+
.catch(e => e);
425+
426+
expect(error)
427+
.to.be.instanceOf(MongoRuntimeError)
428+
.to.match(/No AutoEncrypter available for encryption/);
429+
});
430+
});

0 commit comments

Comments
 (0)