Skip to content

Commit 263beb0

Browse files
wrap FLE errors in MongoCryptErrors
1 parent 23cafe9 commit 263beb0

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

src/client-side-encryption/auto_encrypter.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
type MongoCrypt,
3-
type MongoCryptConstructor,
4-
type MongoCryptOptions
5-
} from 'mongodb-client-encryption';
1+
import { type MongoCrypt, type MongoCryptOptions } from 'mongodb-client-encryption';
62
import * as net from 'net';
73

84
import { deserialize, type Document, serialize } from '../bson';
@@ -15,7 +11,7 @@ import { type Abortable } from '../mongo_types';
1511
import { MongoDBCollectionNamespace } from '../utils';
1612
import { autoSelectSocketOptions } from './client_encryption';
1713
import * as cryptoCallbacks from './crypto_callbacks';
18-
import { MongoCryptInvalidArgumentError } from './errors';
14+
import { defaultErrorWrapper, MongoCryptInvalidArgumentError } from './errors';
1915
import { MongocryptdManager } from './mongocryptd_manager';
2016
import {
2117
type CredentialProviders,
@@ -183,7 +179,7 @@ export class AutoEncrypter {
183179
[kDecorateResult] = false;
184180

185181
/** @internal */
186-
static getMongoCrypt(): MongoCryptConstructor {
182+
static getMongoCrypt(): typeof MongoCrypt {
187183
const encryption = getMongoDBClientEncryption();
188184
if ('kModuleError' in encryption) {
189185
throw encryption.kModuleError;
@@ -259,7 +255,8 @@ export class AutoEncrypter {
259255

260256
const mongoCryptOptions: MongoCryptOptions = {
261257
enableMultipleCollinfo: true,
262-
cryptoCallbacks
258+
cryptoCallbacks,
259+
errorWrapper: defaultErrorWrapper
263260
};
264261
if (options.schemaMap) {
265262
mongoCryptOptions.schemaMap = Buffer.isBuffer(options.schemaMap)

src/client-side-encryption/client_encryption.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {
22
ExplicitEncryptionContextOptions,
33
MongoCrypt,
4-
MongoCryptConstructor,
54
MongoCryptOptions
65
} from 'mongodb-client-encryption';
76

@@ -28,6 +27,7 @@ import { type CSOTTimeoutContext, TimeoutContext } from '../timeout';
2827
import { MongoDBCollectionNamespace, resolveTimeoutOptions } from '../utils';
2928
import * as cryptoCallbacks from './crypto_callbacks';
3029
import {
30+
defaultErrorWrapper,
3131
MongoCryptCreateDataKeyError,
3232
MongoCryptCreateEncryptedCollectionError,
3333
MongoCryptInvalidArgumentError
@@ -87,7 +87,7 @@ export class ClientEncryption {
8787
_credentialProviders?: CredentialProviders;
8888

8989
/** @internal */
90-
static getMongoCrypt(): MongoCryptConstructor {
90+
static getMongoCrypt(): typeof MongoCrypt {
9191
const encryption = getMongoDBClientEncryption();
9292
if ('kModuleError' in encryption) {
9393
throw encryption.kModuleError;
@@ -147,7 +147,8 @@ export class ClientEncryption {
147147
cryptoCallbacks,
148148
kmsProviders: !Buffer.isBuffer(this._kmsProviders)
149149
? (serialize(this._kmsProviders) as Buffer)
150-
: this._kmsProviders
150+
: this._kmsProviders,
151+
errorWrapper: defaultErrorWrapper
151152
};
152153

153154
this._keyVaultNamespace = options.keyVaultNamespace;

src/client-side-encryption/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export class MongoCryptError extends MongoError {
2626
}
2727
}
2828

29+
export const defaultErrorWrapper = (error: Error) =>
30+
new MongoCryptError(error.message, { cause: error });
31+
2932
/**
3033
* @public
3134
*

test/integration/client-side-encryption/client_side_encryption.prose.22.range_explicit_encryption.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ describe('Range Explicit Encryption', function () {
537537
})
538538
.catch(e => e);
539539

540-
expect(resultOrError).to.be.instanceOf(TypeError);
540+
expect(resultOrError).to.be.instanceOf(MongoCryptError);
541541
}
542542
);
543543
});

test/integration/client-side-encryption/client_side_encryption.prose.25.lookup.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import { type MongoCryptOptions } from 'mongodb-client-encryption';
66
import * as sinon from 'sinon';
77

88
import { getCSFLEKMSProviders } from '../../csfle-kms-providers';
9-
import { AutoEncrypter, BSON, type Document, type MongoClient } from '../../mongodb';
9+
import {
10+
AutoEncrypter,
11+
BSON,
12+
type Document,
13+
type MongoClient,
14+
MongoCryptError
15+
} from '../../mongodb';
1016
import { type TestConfiguration } from '../../tools/runner/config';
1117
import { getEncryptExtraOptions } from '../../tools/utils';
1218

@@ -421,7 +427,7 @@ describe('$lookup support', defaultMetadata, function () {
421427
.toArray()
422428
.catch(error => error);
423429

424-
expect(actual).to.be.instanceOf(TypeError);
430+
expect(actual).to.be.instanceOf(MongoCryptError);
425431
expect(actual.message).to.match(
426432
/libmongocrypt is not configured to support encrypting a command with multiple collections/i
427433
);

0 commit comments

Comments
 (0)