Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-6374): MongoOperationTimeoutError inherits MongoRuntimeError #4237

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -310,7 +310,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 @@ -765,9 +765,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