Skip to content

Commit

Permalink
fix(NODE-6374): MongoOperationTimeoutError inherits MongoRuntimeError (
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored and dariakp committed Nov 6, 2024
1 parent 131f6ed commit 9fb896a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
6 changes: 5 additions & 1 deletion etc/notes/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Children of `MongoError` include:
### `MongoDriverError`

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.
Its children are the main classes of errors that most users will interact with: [**`MongoAPIError`**](#MongoAPIError) and [**`MongoRuntimeError`**](#MongoRuntimeError).
Its children are the main classes of errors that most users will interact with: [**`MongoAPIError`**](#MongoAPIError), [**`MongoRuntimeError`**](#MongoRuntimeError) and [**`MongoOperationTimeoutError`**](#MongoOperationTimeoutError).

### `MongoAPIError`

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

### `MongoOperationTimeoutError`

- TODO(NODE-5688): Add MongoOperationTimeoutError documentation

### MongoUnexpectedServerResponseError

Intended for the scenario where the MongoDB returns an unexpected response in relation to some state the driver is in.
Expand Down
21 changes: 18 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class MongoAPIError extends MongoDriverError {

/**
* An error generated when the driver encounters unexpected input
* or reaches an unexpected/invalid internal state
* or reaches an unexpected/invalid internal state.
*
* @privateRemarks
* Should **never** be directly instantiated.
Expand Down Expand Up @@ -861,9 +861,24 @@ export class MongoUnexpectedServerResponseError extends MongoRuntimeError {
}

/**
* @internal
* @public
* @category Error
*
* This error is thrown when an operation could not be completed within the specified `timeoutMS`.
* TODO(NODE-5688): expand this documentation.
*
* @example
* ```ts
* try {
* await blogs.insertOne(blogPost, { timeoutMS: 60_000 })
* } catch (error) {
* if (error instanceof MongoOperationTimeoutError) {
* console.log(`Oh no! writer's block!`, error);
* }
* }
* ```
*/
export class MongoOperationTimeoutError extends MongoRuntimeError {
export class MongoOperationTimeoutError extends MongoDriverError {
override get name(): string {
return 'MongoOperationTimeoutError';
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import {
LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE,
LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE,
MONGODB_ERROR_CODES,
MongoDriverError,
MongoError,
MongoErrorLabel,
MongoMissingDependencyError,
MongoNetworkError,
MongoNetworkTimeoutError,
MongoOperationTimeoutError,
MongoParseError,
MongoRuntimeError,
MongoServerError,
MongoSystemError,
MongoWriteConcernError,
Expand Down Expand Up @@ -173,6 +176,23 @@ describe('MongoErrors', () => {
});
});

describe('class MongoOperationTimeoutError', () => {
it('has a name property equal to MongoOperationTimeoutError', () => {
const error = new MongoOperationTimeoutError('time out!');
expect(error).to.have.property('name', 'MongoOperationTimeoutError');
});

it('is instanceof MongoDriverError', () => {
const error = new MongoOperationTimeoutError('time out!');
expect(error).to.be.instanceOf(MongoDriverError);
});

it('is not instanceof MongoRuntimeError', () => {
const error = new MongoOperationTimeoutError('time out!');
expect(error).to.not.be.instanceOf(MongoRuntimeError);
});
});

describe('MongoMissingDependencyError#constructor', () => {
context('when options.cause is set', () => {
it('attaches the cause property to the instance', () => {
Expand Down

0 comments on commit 9fb896a

Please sign in to comment.