From 96e2a4ead530a41bd803b2852f1a7c3e898764a7 Mon Sep 17 00:00:00 2001 From: bailey Date: Fri, 3 Oct 2025 08:36:41 -0600 Subject: [PATCH 1/2] throw runtime error instead of dependency error --- src/cmap/connection.ts | 7 +------ test/unit/cmap/connection.test.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index ce62f9a7dad..e235c6e2393 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -22,7 +22,6 @@ import { import { MongoCompatibilityError, MONGODB_ERROR_CODES, - MongoMissingDependencyError, MongoNetworkError, MongoNetworkTimeoutError, MongoOperationTimeoutError, @@ -868,11 +867,7 @@ export class CryptoConnection extends Connection { ): Promise { const { autoEncrypter } = this; if (!autoEncrypter) { - // TODO(NODE-6065): throw a MongoRuntimeError in Node V7 - // @ts-expect-error No cause provided because there is no underlying error. - throw new MongoMissingDependencyError('No AutoEncrypter available for encryption', { - dependencyName: 'n/a' - }); + throw new MongoRuntimeError('No AutoEncrypter available for encryption'); } const serverWireVersion = maxWireVersion(this); diff --git a/test/unit/cmap/connection.test.ts b/test/unit/cmap/connection.test.ts index fcd15a5dd38..a605ad35770 100644 --- a/test/unit/cmap/connection.test.ts +++ b/test/unit/cmap/connection.test.ts @@ -6,7 +6,7 @@ import * as sinon from 'sinon'; import { setTimeout } from 'timers/promises'; import { connect } from '../../../src/cmap/connect'; -import { Connection, SizedMessageTransform } from '../../../src/cmap/connection'; +import { Connection, CryptoConnection, SizedMessageTransform } from '../../../src/cmap/connection'; import { MongoNetworkTimeoutError, MongoRuntimeError } from '../../../src/error'; import { MongoClientAuthProviders } from '../../../src/mongo_client_auth_providers'; import { isHello, MongoDBCollectionNamespace, ns, promiseWithResolvers } from '../../../src/utils'; @@ -401,4 +401,18 @@ describe('new Connection()', function () { expect(error).to.be.instanceOf(MongoRuntimeError); }); }); + + it('CryptoConnection.command() throws if no autoEncrypter is configured', async function () { + const error = await connect({ + ...connectionOptionsDefaults, + hostAddress: server.hostAddress(), + authProviders: new MongoClientAuthProviders(), + connectionType: CryptoConnection, + extendedMetadata: Promise.resolve({}) + }).catch(e => e); + + expect(error) + .to.be.instanceOf(MongoRuntimeError) + .to.match(/No AutoEncrypter available for encryption/); + }); }); From 1ff70bc6dd21d5b554d199d086af2b5684bb9592 Mon Sep 17 00:00:00 2001 From: bailey Date: Fri, 3 Oct 2025 10:03:53 -0600 Subject: [PATCH 2/2] comments --- test/unit/cmap/connection.test.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/unit/cmap/connection.test.ts b/test/unit/cmap/connection.test.ts index a605ad35770..bab1422a89f 100644 --- a/test/unit/cmap/connection.test.ts +++ b/test/unit/cmap/connection.test.ts @@ -9,7 +9,14 @@ import { connect } from '../../../src/cmap/connect'; import { Connection, CryptoConnection, SizedMessageTransform } from '../../../src/cmap/connection'; import { MongoNetworkTimeoutError, MongoRuntimeError } from '../../../src/error'; import { MongoClientAuthProviders } from '../../../src/mongo_client_auth_providers'; -import { isHello, MongoDBCollectionNamespace, ns, promiseWithResolvers } from '../../../src/utils'; +import { + HostAddress, + isHello, + MongoDBCollectionNamespace, + MongoDBNamespace, + ns, + promiseWithResolvers +} from '../../../src/utils'; import * as mock from '../../tools/mongodb-mock/index'; const connectionOptionsDefaults = { @@ -401,15 +408,20 @@ describe('new Connection()', function () { expect(error).to.be.instanceOf(MongoRuntimeError); }); }); +}); +describe('class CryptoConnection {}', function () { it('CryptoConnection.command() throws if no autoEncrypter is configured', async function () { - const error = await connect({ + const connection = new CryptoConnection(new Socket(), { ...connectionOptionsDefaults, - hostAddress: server.hostAddress(), + hostAddress: HostAddress.fromString('localhost:27017'), authProviders: new MongoClientAuthProviders(), - connectionType: CryptoConnection, extendedMetadata: Promise.resolve({}) - }).catch(e => e); + }); + + const error = await connection + .command(MongoDBNamespace.fromString('foo.bar'), {}, {}) + .catch(e => e); expect(error) .to.be.instanceOf(MongoRuntimeError)