Skip to content

Commit 9fb896a

Browse files
nbbeekendariakp
authored andcommitted
fix(NODE-6374): MongoOperationTimeoutError inherits MongoRuntimeError (#4237)
1 parent 131f6ed commit 9fb896a

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

etc/notes/errors.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Children of `MongoError` include:
6767
### `MongoDriverError`
6868

6969
This class represents errors which originate in the driver itself or when the user incorrectly uses the driver. This class should **never** be directly instantiated.
70-
Its children are the main classes of errors that most users will interact with: [**`MongoAPIError`**](#MongoAPIError) and [**`MongoRuntimeError`**](#MongoRuntimeError).
70+
Its children are the main classes of errors that most users will interact with: [**`MongoAPIError`**](#MongoAPIError), [**`MongoRuntimeError`**](#MongoRuntimeError) and [**`MongoOperationTimeoutError`**](#MongoOperationTimeoutError).
7171

7272
### `MongoAPIError`
7373

@@ -109,6 +109,10 @@ This class should **never** be directly instantiated.
109109
| **MongoGridFSChunkError** | Thrown when a malformed or invalid chunk is encountered when reading from a GridFS Stream. |
110110
| **MongoUnexpectedServerResponseError** | Thrown when the driver receives a **parsable** response it did not expect from the server. |
111111

112+
### `MongoOperationTimeoutError`
113+
114+
- TODO(NODE-5688): Add MongoOperationTimeoutError documentation
115+
112116
### MongoUnexpectedServerResponseError
113117

114118
Intended for the scenario where the MongoDB returns an unexpected response in relation to some state the driver is in.

src/error.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export class MongoAPIError extends MongoDriverError {
314314

315315
/**
316316
* An error generated when the driver encounters unexpected input
317-
* or reaches an unexpected/invalid internal state
317+
* or reaches an unexpected/invalid internal state.
318318
*
319319
* @privateRemarks
320320
* Should **never** be directly instantiated.
@@ -861,9 +861,24 @@ export class MongoUnexpectedServerResponseError extends MongoRuntimeError {
861861
}
862862

863863
/**
864-
* @internal
864+
* @public
865+
* @category Error
866+
*
867+
* This error is thrown when an operation could not be completed within the specified `timeoutMS`.
868+
* TODO(NODE-5688): expand this documentation.
869+
*
870+
* @example
871+
* ```ts
872+
* try {
873+
* await blogs.insertOne(blogPost, { timeoutMS: 60_000 })
874+
* } catch (error) {
875+
* if (error instanceof MongoOperationTimeoutError) {
876+
* console.log(`Oh no! writer's block!`, error);
877+
* }
878+
* }
879+
* ```
865880
*/
866-
export class MongoOperationTimeoutError extends MongoRuntimeError {
881+
export class MongoOperationTimeoutError extends MongoDriverError {
867882
override get name(): string {
868883
return 'MongoOperationTimeoutError';
869884
}

test/unit/error.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ import {
1414
LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE,
1515
LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE,
1616
MONGODB_ERROR_CODES,
17+
MongoDriverError,
1718
MongoError,
1819
MongoErrorLabel,
1920
MongoMissingDependencyError,
2021
MongoNetworkError,
2122
MongoNetworkTimeoutError,
23+
MongoOperationTimeoutError,
2224
MongoParseError,
25+
MongoRuntimeError,
2326
MongoServerError,
2427
MongoSystemError,
2528
MongoWriteConcernError,
@@ -173,6 +176,23 @@ describe('MongoErrors', () => {
173176
});
174177
});
175178

179+
describe('class MongoOperationTimeoutError', () => {
180+
it('has a name property equal to MongoOperationTimeoutError', () => {
181+
const error = new MongoOperationTimeoutError('time out!');
182+
expect(error).to.have.property('name', 'MongoOperationTimeoutError');
183+
});
184+
185+
it('is instanceof MongoDriverError', () => {
186+
const error = new MongoOperationTimeoutError('time out!');
187+
expect(error).to.be.instanceOf(MongoDriverError);
188+
});
189+
190+
it('is not instanceof MongoRuntimeError', () => {
191+
const error = new MongoOperationTimeoutError('time out!');
192+
expect(error).to.not.be.instanceOf(MongoRuntimeError);
193+
});
194+
});
195+
176196
describe('MongoMissingDependencyError#constructor', () => {
177197
context('when options.cause is set', () => {
178198
it('attaches the cause property to the instance', () => {

0 commit comments

Comments
 (0)