@@ -33,7 +33,6 @@ import {
33
33
MongoRuntimeError ,
34
34
MongoServerClosedError ,
35
35
type MongoServerError ,
36
- MongoUnexpectedServerResponseError ,
37
36
needsRetryableWriteLabel
38
37
} from '../error' ;
39
38
import type { ServerApi } from '../mongo_client' ;
@@ -323,47 +322,17 @@ export class Server extends TypedEventEmitter<ServerEvents> {
323
322
return this . command ( ns , cmd , finalOptions ) ;
324
323
}
325
324
326
- this . incrementOperationCount ( ) ;
327
-
328
- /*
329
- const executeCommand: WithConnectionCallBack = (err, conn, cb) => {
330
- if (err || !conn) {
331
- this.decrementOperationCount();
332
- if (!err) {
333
- return cb(new MongoRuntimeError('Failed to create connection without error'));
334
- }
335
- if (!(err instanceof PoolClearedError)) {
336
- this.handleError(err);
337
- }
338
- return cb(err);
339
- }
340
-
341
- const handler = makeOperationHandler(this, conn, cmd, finalOptions, (error, response) => {
342
- this.decrementOperationCount();
343
- cb(error, response);
344
- });
345
- conn.command(ns, cmd, finalOptions).then(
346
- r => handler(undefined, r),
347
- e => handler(e)
348
- );
349
- };
350
- */
351
-
352
- const executeCommandAsync = async ( err ?: AnyError , conn ?: Connection ) => {
353
- if ( err || ! conn ) {
354
- this . decrementOperationCount ( ) ;
355
- if ( ! err ) throw new MongoRuntimeError ( 'Failed to create connection without error' ) ;
356
- if ( ! ( err instanceof PoolClearedError ) ) this . handleError ( err ) ;
357
-
358
- throw err ;
359
- }
325
+ const runCommand = async ( conn : Connection ) => {
326
+ this . incrementOperationCount ( ) ;
360
327
try {
361
- const result = await conn . command ( ns , cmd , finalOptions ) ;
362
- return await this . operationHandler ( conn , cmd , finalOptions , undefined , result ) ;
328
+ return await conn . command ( ns , cmd , finalOptions ) ;
363
329
} catch ( e ) {
364
- return await this . operationHandler ( conn , cmd , finalOptions , e , undefined ) ;
330
+ throw this . decorateAndHandleError ( conn , cmd , finalOptions , e ) ;
331
+ } finally {
332
+ this . decrementOperationCount ( ) ;
365
333
}
366
334
} ;
335
+
367
336
const shouldReauth = ( e : AnyError ) : boolean => {
368
337
return e instanceof MongoError && e . code === MONGODB_ERROR_CODES . Reauthenticate ;
369
338
} ;
@@ -372,47 +341,39 @@ export class Server extends TypedEventEmitter<ServerEvents> {
372
341
if ( conn ) {
373
342
// send operation, possibly reauthenticating and return without checking back in
374
343
try {
375
- return await executeCommandAsync ( undefined , conn ) ;
344
+ return await runCommand ( conn ) ;
376
345
} catch ( e ) {
377
346
if ( shouldReauth ( e ) ) {
378
347
conn = await this . pool . reauthenticateAsync ( conn ) ;
379
- return await executeCommandAsync ( undefined , conn ) ;
348
+ return await runCommand ( conn ) ;
380
349
}
381
-
382
350
throw e ;
383
351
}
384
352
}
385
353
386
354
// check out connection
387
- conn = await this . pool . checkOut ( ) ;
355
+ try {
356
+ conn = await this . pool . checkOut ( ) ;
357
+ } catch ( e ) {
358
+ if ( ! e ) throw new MongoRuntimeError ( 'Failed to create connection without error' ) ;
359
+ if ( ! ( e instanceof PoolClearedError ) ) this . handleError ( e ) ;
388
360
389
- let rv : Document ;
361
+ throw e ;
362
+ }
390
363
// call executeCommandAsync with that checked out connection
391
364
try {
392
- rv = await executeCommandAsync ( undefined , conn ) ;
365
+ return await runCommand ( conn ) ;
393
366
} catch ( e ) {
394
367
// if it errors
395
- if ( conn ) {
396
- // if connection is defined
397
- // reauthenticate
398
- if ( shouldReauth ( e ) ) {
399
- conn = await this . pool . reauthenticateAsync ( conn ) ;
400
- rv = await executeCommandAsync ( undefined , conn ) ;
401
- } else {
402
- throw e ;
403
- }
404
- } else {
405
- // throw the error
406
- throw e ;
368
+ // check if we should reauthenticate
369
+ if ( shouldReauth ( e ) ) {
370
+ conn = await this . pool . reauthenticateAsync ( conn ) ;
371
+ return await runCommand ( conn ) ;
407
372
}
408
- }
409
-
410
- // if connection exists
411
- // check connection back into pool
412
- if ( conn ) {
373
+ throw e ;
374
+ } finally {
413
375
this . pool . checkIn ( conn ) ;
414
376
}
415
- return rv ;
416
377
}
417
378
418
379
/**
@@ -463,27 +424,13 @@ export class Server extends TypedEventEmitter<ServerEvents> {
463
424
}
464
425
}
465
426
466
- private async operationHandler < T = Document > (
427
+ private decorateAndHandleError (
467
428
connection : Connection ,
468
429
cmd : Document ,
469
430
options : CommandOptions | GetMoreOptions | undefined ,
470
- error ?: AnyError ,
471
- result ?: any
472
- ) : Promise < T > {
431
+ error : AnyError
432
+ ) : AnyError {
473
433
const session = options ?. session ;
474
- // We should not swallow an error if it is present.
475
- if ( error == null && result != null ) {
476
- return result ;
477
- }
478
-
479
- if ( options != null && 'noResponse' in options && options . noResponse === true ) {
480
- return null as T ;
481
- }
482
-
483
- if ( ! error ) {
484
- throw new MongoUnexpectedServerResponseError ( 'Empty response with no error' ) ;
485
- }
486
-
487
434
if ( error . name === 'AbortError' && error . cause instanceof MongoError ) {
488
435
error = error . cause ;
489
436
}
@@ -537,7 +484,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
537
484
538
485
this . handleError ( error , connection ) ;
539
486
540
- throw error ;
487
+ return error ;
541
488
}
542
489
543
490
/**
0 commit comments