Skip to content

Commit a17b0af

Browse files
feat(NODE-5484)!: mark MongoError for internal use and remove Node14 cause assignment logic (#3800)
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
1 parent 33c86c9 commit a17b0af

File tree

12 files changed

+621
-115
lines changed

12 files changed

+621
-115
lines changed

etc/notes/errors.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [`MongoNetworkError`](#MongoNetworkError)
1919
- [`MongoServerError`](#MongoServerError)
2020
- [`MongoSystemError`](#MongoSystemError)
21+
- [`MongoCryptError`](#MongoCryptError)
2122
- [Test Plan](#Test-Plan)
2223
- [`MongoAPIError`](#MongoAPIError-1)
2324
- [`MongoInvalidArgumentError`](#MongoInvalidArgumentError-1)
@@ -32,37 +33,27 @@
3233
## Errors
3334

3435
All errors are derived from the `MongoError` class which should **never** be instantiated.
35-
There are four main error classes which stem from `MongoError`: `MongoDriverError`,
36-
`MongoNetworkError`, `MongoServerError`, and `MongoSystemError`.
36+
There are five main error classes which stem from `MongoError`: `MongoDriverError`,
37+
`MongoNetworkError`, `MongoServerError`, `MongoCryptError` and `MongoSystemError`.
3738

3839
### `MongoError`
3940

4041
The base class from which all errors in the Node driver subclass.
4142
`MongoError` should **never** be be directly instantiated.
4243

4344
```mermaid
45+
4446
graph TD
45-
MongoError --- MongoDriverError
46-
MongoError --- MongoNetworkError
47-
MongoError --- MongoServerError
48-
MongoError --- MongoSystemError
49-
MongoDriverError --- MongoAPIError
50-
MongoDriverError --- MongoRuntimeError
51-
52-
linkStyle 0 stroke:#116149
53-
linkStyle 1 stroke:#116149
54-
linkStyle 2 stroke:#116149
55-
linkStyle 3 stroke:#116149
56-
linkStyle 4 stroke:#116149
57-
linkStyle 5 stroke:#116149
58-
59-
style MongoError fill:#13aa52,stroke:#21313c,color:#FAFBFC
60-
style MongoSystemError fill:#13aa52,stroke:#21313c,color:#FAFBFC
61-
style MongoNetworkError fill:#13aa52,stroke:#21313c,color:#FAFBFC
62-
style MongoServerError fill:#13aa52,stroke:#21313c,color:#FAFBFC
63-
style MongoDriverError fill:#13aa52,stroke:#21313c,color:#FAFBFC
64-
style MongoAPIError fill:#13aa52,stroke:#21313c,color:#FAFBFC
65-
style MongoRuntimeError fill:#13aa52,stroke:#21313c,color:#FAFBFC
47+
MongoError:::node --- MongoDriverError
48+
MongoError:::node --- MongoNetworkError
49+
MongoError:::node --- MongoServerError
50+
MongoError:::node --- MongoSystemError
51+
MongoError:::node --- MongoCryptError
52+
MongoDriverError:::node --- MongoAPIError
53+
MongoDriverError:::node --- MongoRuntimeError
54+
55+
linkStyle 0,1,2,3,4,5,6 stroke:#116149
56+
classDef node fill:#13aa52,stroke:#21313c,color:#FAFBFC
6657
```
6758

6859
Children of `MongoError` include:
@@ -71,6 +62,7 @@ Children of `MongoError` include:
7162
- [`MongoNetworkError`](#MongoNetworkError)
7263
- [`MongoServerError`](#MongoServerError)
7364
- [`MongoSystemError`](#MongoSystemError)
65+
- [`MongoCryptError`](#MongoCryptError)
7466

7567
### `MongoDriverError`
7668

@@ -144,6 +136,21 @@ These are errors which originate from faulty environment setup.
144136
- #### MongoServerSelectionError
145137
- Thrown when the driver fails to select a server to complete an operation
146138

139+
### `MongoCryptError`
140+
141+
These are errors thrown from the driver's client side encryption logic.
142+
143+
- #### MongoCryptInvalidArgumentError
144+
- Thrown when an invalid argument has been provided to an encryption API
145+
- #### MongoCryptInvalidCreateDataKeyError
146+
- Thrown when the driver fails to create data keys for an encrypted collection
147+
- #### MongoCryptInvalidCreateEncryptedCollectionError
148+
- Thrown when the driver fails to create an encrypted collection
149+
- #### MongoCryptInvalidCreateAzureKMSRequestError
150+
- Thrown when the driver encounters an error when fetching Azure KMS credentials
151+
- #### MongoCryptKMSRequestNetworkTimeoutError
152+
- Thrown when the HTTP request to the IDMS server times out when fetching Azure KMS credentials
153+
147154
## Test Plan
148155

149156
The test plan consists of a series of prose tests.

src/bulk/common.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,17 @@ export class MongoBulkWriteError extends MongoServerError {
598598
writeErrors: OneOrMore<WriteError> = [];
599599
err?: WriteConcernError;
600600

601-
/** Creates a new MongoBulkWriteError */
601+
/**
602+
* **Do not use this constructor!**
603+
*
604+
* Meant for internal use only.
605+
*
606+
* @remarks
607+
* This class is only meant to be constructed within the driver. This constructor is
608+
* not subject to semantic versioning compatibility guarantees and may change at any time.
609+
*
610+
* @public
611+
**/
602612
constructor(
603613
error:
604614
| { message: string; code: number; writeErrors?: WriteError[] }

src/client-side-encryption/auto_encrypter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ export class AutoEncrypter {
422422
) {
423423
callback(
424424
new MongoRuntimeError(
425-
'Unable to connect to `mongocryptd`, please make sure it is running or in your PATH for auto-spawn'
425+
'Unable to connect to `mongocryptd`, please make sure it is running or in your PATH for auto-spawn',
426+
{ cause: err }
426427
)
427428
);
428429
return;

src/client-side-encryption/errors.ts

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { type Document } from '../bson';
2+
import { MongoError } from '../error';
23

34
/**
45
* @public
56
* An error indicating that something went wrong specifically with MongoDB Client Encryption
67
*/
7-
export class MongoCryptError extends Error {
8-
/** @internal */
8+
export class MongoCryptError extends MongoError {
9+
/**
10+
* **Do not use this constructor!**
11+
*
12+
* Meant for internal use only.
13+
*
14+
* @remarks
15+
* This class is only meant to be constructed within the driver. This constructor is
16+
* not subject to semantic versioning compatibility guarantees and may change at any time.
17+
*
18+
* @public
19+
**/
920
constructor(message: string, options: { cause?: Error } = {}) {
1021
super(message, options);
1122
}
@@ -21,7 +32,17 @@ export class MongoCryptError extends Error {
2132
* An error indicating an invalid argument was provided to an encryption API.
2233
*/
2334
export class MongoCryptInvalidArgumentError extends MongoCryptError {
24-
/** @internal */
35+
/**
36+
* **Do not use this constructor!**
37+
*
38+
* Meant for internal use only.
39+
*
40+
* @remarks
41+
* This class is only meant to be constructed within the driver. This constructor is
42+
* not subject to semantic versioning compatibility guarantees and may change at any time.
43+
*
44+
* @public
45+
**/
2546
constructor(message: string) {
2647
super(message);
2748
}
@@ -36,7 +57,17 @@ export class MongoCryptInvalidArgumentError extends MongoCryptError {
3657
*/
3758
export class MongoCryptCreateDataKeyError extends MongoCryptError {
3859
encryptedFields: Document;
39-
/** @internal */
60+
/**
61+
* **Do not use this constructor!**
62+
*
63+
* Meant for internal use only.
64+
*
65+
* @remarks
66+
* This class is only meant to be constructed within the driver. This constructor is
67+
* not subject to semantic versioning compatibility guarantees and may change at any time.
68+
*
69+
* @public
70+
**/
4071
constructor(encryptedFields: Document, { cause }: { cause: Error }) {
4172
super(`Unable to complete creating data keys: ${cause.message}`, { cause });
4273
this.encryptedFields = encryptedFields;
@@ -53,7 +84,17 @@ export class MongoCryptCreateDataKeyError extends MongoCryptError {
5384
*/
5485
export class MongoCryptCreateEncryptedCollectionError extends MongoCryptError {
5586
encryptedFields: Document;
56-
/** @internal */
87+
/**
88+
* **Do not use this constructor!**
89+
*
90+
* Meant for internal use only.
91+
*
92+
* @remarks
93+
* This class is only meant to be constructed within the driver. This constructor is
94+
* not subject to semantic versioning compatibility guarantees and may change at any time.
95+
*
96+
* @public
97+
**/
5798
constructor(encryptedFields: Document, { cause }: { cause: Error }) {
5899
super(`Unable to create collection: ${cause.message}`, { cause });
59100
this.encryptedFields = encryptedFields;
@@ -71,7 +112,17 @@ export class MongoCryptCreateEncryptedCollectionError extends MongoCryptError {
71112
export class MongoCryptAzureKMSRequestError extends MongoCryptError {
72113
/** The body of the http response that failed, if present. */
73114
body?: Document;
74-
/** @internal */
115+
/**
116+
* **Do not use this constructor!**
117+
*
118+
* Meant for internal use only.
119+
*
120+
* @remarks
121+
* This class is only meant to be constructed within the driver. This constructor is
122+
* not subject to semantic versioning compatibility guarantees and may change at any time.
123+
*
124+
* @public
125+
**/
75126
constructor(message: string, body?: Document) {
76127
super(message);
77128
this.body = body;

src/cmap/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ function makeSocks5Connection(options: MakeConnectionOptions, callback: Callback
502502
function connectionFailureError(type: ErrorHandlerEventName, err: Error) {
503503
switch (type) {
504504
case 'error':
505-
return new MongoNetworkError(err);
505+
return new MongoNetworkError(MongoError.buildErrorMessage(err), { cause: err });
506506
case 'timeout':
507507
return new MongoNetworkTimeoutError('connection timed out');
508508
case 'close':

src/cmap/errors.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ export class PoolClosedError extends MongoDriverError {
99
/** The address of the connection pool */
1010
address: string;
1111

12+
/**
13+
* **Do not use this constructor!**
14+
*
15+
* Meant for internal use only.
16+
*
17+
* @remarks
18+
* This class is only meant to be constructed within the driver. This constructor is
19+
* not subject to semantic versioning compatibility guarantees and may change at any time.
20+
*
21+
* @public
22+
**/
1223
constructor(pool: ConnectionPool) {
1324
super('Attempted to check out a connection from closed connection pool');
1425
this.address = pool.address;
@@ -27,11 +38,22 @@ export class PoolClearedError extends MongoNetworkError {
2738
/** The address of the connection pool */
2839
address: string;
2940

41+
/**
42+
* **Do not use this constructor!**
43+
*
44+
* Meant for internal use only.
45+
*
46+
* @remarks
47+
* This class is only meant to be constructed within the driver. This constructor is
48+
* not subject to semantic versioning compatibility guarantees and may change at any time.
49+
*
50+
* @public
51+
**/
3052
constructor(pool: ConnectionPool, message?: string) {
3153
const errorMessage = message
3254
? message
3355
: `Connection pool for ${pool.address} was cleared because another operation failed with: "${pool.serverError?.message}"`;
34-
super(errorMessage);
56+
super(errorMessage, pool.serverError ? { cause: pool.serverError } : undefined);
3557
this.address = pool.address;
3658

3759
this.addErrorLabel(MongoErrorLabel.RetryableWriteError);
@@ -47,6 +69,17 @@ export class PoolClearedError extends MongoNetworkError {
4769
* @category Error
4870
*/
4971
export class PoolClearedOnNetworkError extends PoolClearedError {
72+
/**
73+
* **Do not use this constructor!**
74+
*
75+
* Meant for internal use only.
76+
*
77+
* @remarks
78+
* This class is only meant to be constructed within the driver. This constructor is
79+
* not subject to semantic versioning compatibility guarantees and may change at any time.
80+
*
81+
* @public
82+
**/
5083
constructor(pool: ConnectionPool) {
5184
super(pool, `Connection to ${pool.address} interrupted due to server monitor timeout`);
5285
}
@@ -64,6 +97,17 @@ export class WaitQueueTimeoutError extends MongoDriverError {
6497
/** The address of the connection pool */
6598
address: string;
6699

100+
/**
101+
* **Do not use this constructor!**
102+
*
103+
* Meant for internal use only.
104+
*
105+
* @remarks
106+
* This class is only meant to be constructed within the driver. This constructor is
107+
* not subject to semantic versioning compatibility guarantees and may change at any time.
108+
*
109+
* @public
110+
**/
67111
constructor(message: string, address: string) {
68112
super(message);
69113
this.address = address;

0 commit comments

Comments
 (0)