Skip to content

Commit 558d416

Browse files
authored
fix(NODE-6374): MongoOperationTimeoutError inherits MongoRuntimeError (#4237)
1 parent 4c4b0a9 commit 558d416

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

etc/notes/errors.md

Lines changed: 5 additions & 1 deletion
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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export class MongoAPIError extends MongoDriverError {
310310

311311
/**
312312
* An error generated when the driver encounters unexpected input
313-
* or reaches an unexpected/invalid internal state
313+
* or reaches an unexpected/invalid internal state.
314314
*
315315
* @privateRemarks
316316
* Should **never** be directly instantiated.
@@ -765,9 +765,24 @@ export class MongoUnexpectedServerResponseError extends MongoRuntimeError {
765765
}
766766

767767
/**
768-
* @internal
768+
* @public
769+
* @category Error
770+
*
771+
* This error is thrown when an operation could not be completed within the specified `timeoutMS`.
772+
* TODO(NODE-5688): expand this documentation.
773+
*
774+
* @example
775+
* ```ts
776+
* try {
777+
* await blogs.insertOne(blogPost, { timeoutMS: 60_000 })
778+
* } catch (error) {
779+
* if (error instanceof MongoOperationTimeoutError) {
780+
* console.log(`Oh no! writer's block!`, error);
781+
* }
782+
* }
783+
* ```
769784
*/
770-
export class MongoOperationTimeoutError extends MongoRuntimeError {
785+
export class MongoOperationTimeoutError extends MongoDriverError {
771786
override get name(): string {
772787
return 'MongoOperationTimeoutError';
773788
}

test/unit/error.test.ts

Lines changed: 20 additions & 0 deletions
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)