Skip to content

Commit 5de94ee

Browse files
committed
feat: remove MongoAbortError
1 parent f8c802e commit 5de94ee

File tree

10 files changed

+210
-202
lines changed

10 files changed

+210
-202
lines changed

src/client-side-encryption/state_machine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,9 @@ export class StateMachine {
428428
resolve
429429
} = promiseWithResolvers<void>();
430430

431-
abortListener = addAbortListener(options?.signal, error => {
431+
abortListener = addAbortListener(options?.signal, function () {
432432
destroySockets();
433-
rejectOnTlsSocketError(error);
433+
rejectOnTlsSocketError(this.reason);
434434
});
435435

436436
socket

src/cmap/connection_pool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
342342
checkoutTime
343343
};
344344

345-
const abortListener = addAbortListener(options.signal, error => {
345+
const abortListener = addAbortListener(options.signal, function () {
346346
waitQueueMember.cancelled = true;
347-
reject(error);
347+
reject(this.reason);
348348
});
349349

350350
this.waitQueue.push(waitQueueMember);

src/cmap/wire_protocol/on_data.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export function onData(
9191
// Adding event handlers
9292
emitter.on('data', eventHandler);
9393
emitter.on('error', errorHandler);
94-
const abortListener = addAbortListener(signal, errorHandler);
94+
const abortListener = addAbortListener(signal, function () {
95+
errorHandler(this.reason);
96+
});
9597

9698
const timeoutForSocketRead = timeoutContext?.timeoutForSocketRead;
9799
timeoutForSocketRead?.throwIfExpired();

src/cursor/abstract_cursor.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import {
2727
type Disposable,
2828
kDispose,
2929
type MongoDBNamespace,
30-
squashError,
31-
throwIfAborted
30+
squashError
3231
} from '../utils';
3332

3433
/**
@@ -467,7 +466,7 @@ export abstract class AbstractCursor<
467466
}
468467

469468
async *[Symbol.asyncIterator](): AsyncGenerator<TSchema, void, void> {
470-
throwIfAborted(this.signal);
469+
this.signal?.throwIfAborted();
471470

472471
if (this.closed) {
473472
return;
@@ -495,7 +494,7 @@ export abstract class AbstractCursor<
495494
}
496495

497496
yield document;
498-
throwIfAborted(this.signal);
497+
this.signal?.throwIfAborted();
499498
}
500499
} finally {
501500
// Only close the cursor if it has not already been closed. This finally clause handles
@@ -541,7 +540,7 @@ export abstract class AbstractCursor<
541540
}
542541

543542
async hasNext(): Promise<boolean> {
544-
throwIfAborted(this.signal);
543+
this.signal?.throwIfAborted();
545544

546545
if (this.cursorId === Long.ZERO) {
547546
return false;
@@ -568,7 +567,7 @@ export abstract class AbstractCursor<
568567

569568
/** Get the next available document from the cursor, returns null if no more documents are available. */
570569
async next(): Promise<TSchema | null> {
571-
throwIfAborted(this.signal);
570+
this.signal?.throwIfAborted();
572571

573572
if (this.cursorId === Long.ZERO) {
574573
throw new MongoCursorExhaustedError();
@@ -600,7 +599,7 @@ export abstract class AbstractCursor<
600599
* Try to get the next available document from the cursor or `null` if an empty batch is returned
601600
*/
602601
async tryNext(): Promise<TSchema | null> {
603-
throwIfAborted(this.signal);
602+
this.signal?.throwIfAborted();
604603

605604
if (this.cursorId === Long.ZERO) {
606605
throw new MongoCursorExhaustedError();
@@ -641,7 +640,7 @@ export abstract class AbstractCursor<
641640
* @deprecated - Will be removed in a future release. Use for await...of instead.
642641
*/
643642
async forEach(iterator: (doc: TSchema) => boolean | void): Promise<void> {
644-
throwIfAborted(this.signal);
643+
this.signal?.throwIfAborted();
645644

646645
if (typeof iterator !== 'function') {
647646
throw new MongoInvalidArgumentError('Argument "iterator" must be a function');
@@ -668,7 +667,7 @@ export abstract class AbstractCursor<
668667
* cursor.rewind() can be used to reset the cursor.
669668
*/
670669
async toArray(): Promise<TSchema[]> {
671-
throwIfAborted(this.signal);
670+
this.signal?.throwIfAborted();
672671

673672
const array: TSchema[] = [];
674673
// at the end of the loop (since readBufferedDocuments is called) the buffer will be empty

src/error.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -196,34 +196,6 @@ export class MongoError extends Error {
196196
}
197197
}
198198

199-
/**
200-
* An error thrown when a signal is aborted
201-
*
202-
* A MongoAbortError has the name "AbortError" to match the name
203-
* given to a DOMException thrown from web APIs that support AbortSignals.
204-
*
205-
* @example
206-
* ```js
207-
* try {
208-
* const res = await fetch('...', { signal });
209-
* await collection.insertOne(await res.json(), { signal });
210-
* catch (error) {
211-
* if (error.name === 'AbortError') {
212-
* // error is MongoAbortError or DOMException,
213-
* // both represent the signal being aborted
214-
* }
215-
* }
216-
* ```
217-
*
218-
* @public
219-
* @category Error
220-
*/
221-
export class MongoAbortError extends MongoError {
222-
override get name(): 'AbortError' {
223-
return 'AbortError';
224-
}
225-
}
226-
227199
/**
228200
* An error coming from the mongo server
229201
*

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export {
4545
export { ClientEncryption } from './client-side-encryption/client_encryption';
4646
export { ChangeStreamCursor } from './cursor/change_stream_cursor';
4747
export {
48-
MongoAbortError,
4948
MongoAPIError,
5049
MongoAWSError,
5150
MongoAzureError,

src/sdam/topology.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,9 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
604604
previousServer: options.previousServer
605605
};
606606

607-
const abortListener = addAbortListener(options.signal, error => {
607+
const abortListener = addAbortListener(options.signal, function () {
608608
waitQueueMember.cancelled = true;
609-
reject(error);
609+
reject(this.reason);
610610
});
611611

612612
this.waitQueue.push(waitQueueMember);

src/utils.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import type { FindCursor } from './cursor/find_cursor';
1818
import type { Db } from './db';
1919
import {
2020
type AnyError,
21-
MongoAbortError,
2221
MongoAPIError,
2322
MongoCompatibilityError,
2423
MongoInvalidArgumentError,
@@ -1355,7 +1354,9 @@ export async function once<T>(ee: EventEmitter, name: string, options?: Abortabl
13551354
const { promise, resolve, reject } = promiseWithResolvers<T>();
13561355
const onEvent = (data: T) => resolve(data);
13571356
const onError = (error: Error) => reject(error);
1358-
const abortListener = addAbortListener(options?.signal, reject);
1357+
const abortListener = addAbortListener(options?.signal, function () {
1358+
reject(this.reason);
1359+
});
13591360

13601361
ee.once(name, onEvent).once('error', onError);
13611362

@@ -1479,20 +1480,9 @@ export interface Disposable {
14791480

14801481
export function addAbortListener(
14811482
signal: AbortSignal | undefined | null,
1482-
listener: (event: Error) => void
1483+
listener: (this: AbortSignal, event: Event) => void
14831484
): Disposable | undefined {
14841485
if (signal == null) return;
1485-
1486-
const convertReasonToError = () =>
1487-
listener(new MongoAbortError('Operation was aborted', { cause: signal.reason }));
1488-
1489-
signal.addEventListener('abort', convertReasonToError);
1490-
1491-
return { [kDispose]: () => signal.removeEventListener('abort', convertReasonToError) };
1492-
}
1493-
1494-
export function throwIfAborted(signal?: { aborted?: boolean; reason?: any }): void {
1495-
if (signal?.aborted) {
1496-
throw new MongoAbortError('Operation was aborted', { cause: signal.reason });
1497-
}
1486+
signal.addEventListener('abort', listener);
1487+
return { [kDispose]: () => signal.removeEventListener('abort', listener) };
14981488
}

0 commit comments

Comments
 (0)