Skip to content

Commit

Permalink
refactor(NODE-3421): replace MongoDriverError instances with MongoRun…
Browse files Browse the repository at this point in the history
…timeError children (#2910)

* refactor: Replace MongoDriverError instances

* refactor: Use MongoCursorInUseError

* refactor: Change error message

* refactor: Change message for MongoBatchReExecutionError instance

* refactor: Give default error message to special-case error classes

* test: Fix test failure related to error message

* style: condense constructors

* chore: remove unused import

* fix: Update error class parents to better reflect when user is at fault

* refactor: Remove MongoResourseClosedError

* test: change to be an instanceof check
  • Loading branch information
W-A-James authored Jul 30, 2021
1 parent b42a1b4 commit a9c0de8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 124 deletions.
6 changes: 3 additions & 3 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
AnyError,
MONGODB_ERROR_CODES,
MongoServerError,
MongoDriverError,
MongoInvalidArgumentError
MongoInvalidArgumentError,
MongoBatchReExecutionError
} from '../error';
import {
applyRetryableWrites,
Expand Down Expand Up @@ -1181,7 +1181,7 @@ export abstract class BulkOperationBase {
options = options ?? {};

if (this.s.executed) {
return handleEarlyError(new MongoDriverError('Batch cannot be re-executed'), callback);
return handleEarlyError(new MongoBatchReExecutionError(), callback);
}

const writeConcern = WriteConcern.fromOptions(options);
Expand Down
16 changes: 11 additions & 5 deletions src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Callback, maybePromise, MongoDBNamespace, ns } from '../utils';
import { Long, Document, BSONSerializeOptions, pluckBSONSerializeOptions } from '../bson';
import { ClientSession } from '../sessions';
import { MongoDriverError, MongoInvalidArgumentError } from '../error';
import {
MongoDriverError,
MongoInvalidArgumentError,
MongoCursorExhaustedError,
MongoTailableCursorError,
MongoCursorInUseError
} from '../error';
import { ReadPreference, ReadPreferenceLike } from '../read_preference';
import type { Server } from '../sdam/server';
import type { Topology } from '../sdam/topology';
Expand Down Expand Up @@ -287,7 +293,7 @@ export abstract class AbstractCursor<
next<T = TSchema>(callback?: Callback<T | null>): Promise<T | null> | void {
return maybePromise(callback, done => {
if (this[kId] === Long.ZERO) {
return done(new MongoDriverError('Cursor is exhausted'));
return done(new MongoCursorExhaustedError());
}

next(this, true, done);
Expand All @@ -302,7 +308,7 @@ export abstract class AbstractCursor<
tryNext<T = TSchema>(callback?: Callback<T | null>): Promise<T | null> | void {
return maybePromise(callback, done => {
if (this[kId] === Long.ZERO) {
return done(new MongoDriverError('Cursor is exhausted'));
return done(new MongoCursorExhaustedError());
}

next(this, false, done);
Expand Down Expand Up @@ -564,7 +570,7 @@ export abstract class AbstractCursor<
batchSize(value: number): this {
assertUninitialized(this);
if (this[kOptions].tailable) {
throw new MongoDriverError('Tailable cursors do not support batchSize');
throw new MongoTailableCursorError('Tailable cursor does not support batchSize');
}

if (typeof value !== 'number') {
Expand Down Expand Up @@ -777,7 +783,7 @@ function cleanupCursor(cursor: AbstractCursor, callback: Callback): void {
/** @internal */
export function assertUninitialized(cursor: AbstractCursor): void {
if (cursor[kInitialized]) {
throw new MongoDriverError('Cursor is already initialized');
throw new MongoCursorInUseError();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Document } from '../bson';
import { MongoDriverError, MongoInvalidArgumentError } from '../error';
import { MongoInvalidArgumentError, MongoTailableCursorError } from '../error';
import type { ExplainVerbosityLike } from '../explain';
import { CountOperation, CountOptions } from '../operations/count';
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
Expand Down Expand Up @@ -371,7 +371,7 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
sort(sort: Sort | string, direction?: SortDirection): this {
assertUninitialized(this);
if (this[kBuiltOptions].tailable) {
throw new MongoDriverError('Tailable cursor does not support sorting');
throw new MongoTailableCursorError('Tailable cursor does not support sorting');
}

this[kBuiltOptions].sort = formatSort(sort, direction);
Expand Down Expand Up @@ -412,7 +412,7 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
limit(value: number): this {
assertUninitialized(this);
if (this[kBuiltOptions].tailable) {
throw new MongoDriverError('Tailable cursor does not support limit');
throw new MongoTailableCursorError('Tailable cursor does not support limit');
}

if (typeof value !== 'number') {
Expand All @@ -431,7 +431,7 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
skip(value: number): this {
assertUninitialized(this);
if (this[kBuiltOptions].tailable) {
throw new MongoDriverError('Tailable cursor does not support skip');
throw new MongoTailableCursorError('Tailable cursor does not support skip');
}

if (typeof value !== 'number') {
Expand Down
147 changes: 40 additions & 107 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ export class MongoDriverError extends MongoError {
}
}

/**
* An error generated when the driver API is used incorrectly
*
* @privateRemarks
* Should **never** be directly instantiated
*
* @public
* @category Error
*/

export class MongoAPIError extends MongoDriverError {
protected constructor(message: string) {
super(message);
}

get name(): string {
return 'MongoAPIError';
}
}

/**
* An error generated when the driver encounters unexpected input
* or reaches an unexpected/invalid internal state
Expand Down Expand Up @@ -205,9 +225,9 @@ export class MongoRuntimeError extends MongoDriverError {
* @public
* @category Error
*/
export class MongoBatchReExecutionError extends MongoRuntimeError {
constructor(message: string) {
super(message);
export class MongoBatchReExecutionError extends MongoAPIError {
constructor(message?: string) {
super(message || 'This batch has already been executed, create new batch to execute');
}

get name(): string {
Expand Down Expand Up @@ -272,7 +292,7 @@ export class MongoDecompressionError extends MongoRuntimeError {
* @public
* @category Error
*/
export class MongoNotConnectedError extends MongoRuntimeError {
export class MongoNotConnectedError extends MongoAPIError {
constructor(message: string) {
super(message);
}
Expand All @@ -289,7 +309,7 @@ export class MongoNotConnectedError extends MongoRuntimeError {
* @public
* @category Error
*/
export class MongoTransactionError extends MongoRuntimeError {
export class MongoTransactionError extends MongoAPIError {
constructor(message: string) {
super(message);
}
Expand All @@ -306,7 +326,7 @@ export class MongoTransactionError extends MongoRuntimeError {
* @public
* @category Error
*/
export class MongoExpiredSessionError extends MongoRuntimeError {
export class MongoExpiredSessionError extends MongoAPIError {
constructor(message: string) {
super(message);
}
Expand All @@ -333,46 +353,13 @@ export class MongoKerberosError extends MongoRuntimeError {
}
}

/**
* An error thrown when the user attempts to operate on a cursor that is in a state which does not
* support the attempted operation.
*
* @public
* @category Error
*/
export class MongoCursorError extends MongoRuntimeError {
constructor(message: string) {
super(message);
}

get name(): string {
return 'MongoCursorError';
}
}

/**
* An error generated when a stream operation fails to execute.
*
* @public
* @category Error
*/
export class MongoStreamError extends MongoRuntimeError {
constructor(message: string) {
super(message);
}

get name(): string {
return 'MongoStreamError';
}
}

/**
* An error generated when a ChangeStream operation fails to execute.
*
* @public
* @category Error
*/
export class MongoChangeStreamError extends MongoStreamError {
export class MongoChangeStreamError extends MongoRuntimeError {
constructor(message: string) {
super(message);
}
Expand All @@ -388,9 +375,9 @@ export class MongoChangeStreamError extends MongoStreamError {
* @public
* @category Error
*/
export class MongoTailableCursorError extends MongoCursorError {
constructor(message: string) {
super(message);
export class MongoTailableCursorError extends MongoAPIError {
constructor(message?: string) {
super(message || 'Tailable cursor does not support this operation');
}

get name(): string {
Expand All @@ -403,7 +390,7 @@ export class MongoTailableCursorError extends MongoCursorError {
* @public
* @category Error
*/
export class MongoGridFSStreamError extends MongoStreamError {
export class MongoGridFSStreamError extends MongoRuntimeError {
constructor(message: string) {
super(message);
}
Expand All @@ -420,7 +407,7 @@ export class MongoGridFSStreamError extends MongoStreamError {
* @public
* @category Error
*/
export class MongoGridFSChunkError extends MongoStreamError {
export class MongoGridFSChunkError extends MongoRuntimeError {
constructor(message: string) {
super(message);
}
Expand All @@ -437,41 +424,24 @@ export class MongoGridFSChunkError extends MongoStreamError {
* @public
* @category Error
*/
export class MongoCursorInUseError extends MongoCursorError {
constructor(message: string) {
super(message);
export class MongoCursorInUseError extends MongoAPIError {
constructor(message?: string) {
super(message || 'Cursor is already initialized');
}

get name(): string {
return 'MongoCursorInUseError';
}
}

/**
* An error generated when an attempt is made to access a resource
* which has already been or will be closed/destroyed.
*
* @public
* @category Error
*/
export class MongoResourceClosedError extends MongoRuntimeError {
constructor(message: string) {
super(message);
}

get name(): string {
return 'MongoResourceClosedError';
}
}

/**
* An error generated when an attempt is made to operate
* on a closed/closing server.
*
* @public
* @category Error
*/
export class MongoServerClosedError extends MongoResourceClosedError {
export class MongoServerClosedError extends MongoAPIError {
constructor(message: string) {
super(message);
}
Expand All @@ -487,41 +457,24 @@ export class MongoServerClosedError extends MongoResourceClosedError {
* @public
* @category Error
*/
export class MongoCursorExhaustedError extends MongoCursorError {
constructor(message: string) {
super(message);
export class MongoCursorExhaustedError extends MongoAPIError {
constructor(message?: string) {
super(message || 'Cursor is exhausted');
}

get name(): string {
return 'MongoCursorExhaustedError';
}
}

/**
* An error generated when an attempt is made to operate
* on a closed/closing stream.
*
* @public
* @category Error
*/
export class MongoStreamClosedError extends MongoResourceClosedError {
constructor(message: string) {
super(message);
}

get name(): string {
return 'MongoStreamClosedError';
}
}

/**
* An error generated when an attempt is made to operate on a
* dropped, or otherwise unavailable, database.
*
* @public
* @category Error
*/
export class MongoTopologyClosedError extends MongoResourceClosedError {
export class MongoTopologyClosedError extends MongoAPIError {
constructor(message: string) {
super(message);
}
Expand Down Expand Up @@ -598,26 +551,6 @@ export class MongoParseError extends MongoDriverError {
}
}

/**
* An error generated when the driver API is used incorrectly
*
* @privateRemarks
* Should **never** be directly instantiated
*
* @public
* @category Error
*/

export class MongoAPIError extends MongoDriverError {
protected constructor(message: string) {
super(message);
}

get name(): string {
return 'MongoAPIError';
}
}

/**
* An error generated when the user supplies malformed or unexpected arguments
* or when a required argument or field is not provided.
Expand Down
9 changes: 7 additions & 2 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Db, DbOptions } from './db';
import { ChangeStream, ChangeStreamOptions } from './change_stream';
import type { ReadPreference, ReadPreferenceMode } from './read_preference';
import { AnyError, MongoDriverError, MongoInvalidArgumentError } from './error';
import {
AnyError,
MongoDriverError,
MongoInvalidArgumentError,
MongoNotConnectedError
} from './error';
import type { W, WriteConcern } from './write_concern';
import {
maybePromise,
Expand Down Expand Up @@ -521,7 +526,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
startSession(options?: ClientSessionOptions): ClientSession {
options = Object.assign({ explicit: true }, options);
if (!this.topology) {
throw new MongoDriverError('Must connect to a server before calling this method');
throw new MongoNotConnectedError('MongoClient must be connected to start a session');
}

return this.topology.startSession(options, this.s.options);
Expand Down
Loading

0 comments on commit a9c0de8

Please sign in to comment.