@@ -279,20 +279,7 @@ class Server extends EventEmitter {
279
279
280
280
this . s . pool . withConnection ( ( err , conn , cb ) => {
281
281
if ( err ) return cb ( err ) ;
282
-
283
- conn . command ( ns , cmd , options , ( err , result ) => {
284
- if ( err ) {
285
- if ( options . session && err instanceof MongoNetworkError ) {
286
- options . session . serverSession . isDirty = true ;
287
- }
288
-
289
- if ( isSDAMUnrecoverableError ( err ) ) {
290
- this . emit ( 'error' , err ) ;
291
- }
292
- }
293
-
294
- cb ( err , result ) ;
295
- } ) ;
282
+ conn . command ( ns , cmd , options , makeOperationHandler ( this , options , cb ) ) ;
296
283
} , callback ) ;
297
284
}
298
285
@@ -312,20 +299,7 @@ class Server extends EventEmitter {
312
299
313
300
this . s . pool . withConnection ( ( err , conn , cb ) => {
314
301
if ( err ) return cb ( err ) ;
315
-
316
- conn . query ( ns , cmd , cursorState , options , ( err , result ) => {
317
- if ( err ) {
318
- if ( options . session && err instanceof MongoNetworkError ) {
319
- options . session . serverSession . isDirty = true ;
320
- }
321
-
322
- if ( isSDAMUnrecoverableError ( err ) ) {
323
- this . emit ( 'error' , err ) ;
324
- }
325
- }
326
-
327
- cb ( err , result ) ;
328
- } ) ;
302
+ conn . query ( ns , cmd , cursorState , options , makeOperationHandler ( this , options , cb ) ) ;
329
303
} , callback ) ;
330
304
}
331
305
@@ -345,20 +319,7 @@ class Server extends EventEmitter {
345
319
346
320
this . s . pool . withConnection ( ( err , conn , cb ) => {
347
321
if ( err ) return cb ( err ) ;
348
-
349
- conn . getMore ( ns , cursorState , batchSize , options , ( err , result ) => {
350
- if ( err ) {
351
- if ( options . session && err instanceof MongoNetworkError ) {
352
- options . session . serverSession . isDirty = true ;
353
- }
354
-
355
- if ( isSDAMUnrecoverableError ( err ) ) {
356
- this . emit ( 'error' , err ) ;
357
- }
358
- }
359
-
360
- cb ( err , result ) ;
361
- } ) ;
322
+ conn . getMore ( ns , cursorState , batchSize , options , makeOperationHandler ( this , options , cb ) ) ;
362
323
} , callback ) ;
363
324
}
364
325
@@ -380,14 +341,7 @@ class Server extends EventEmitter {
380
341
381
342
this . s . pool . withConnection ( ( err , conn , cb ) => {
382
343
if ( err ) return cb ( err ) ;
383
-
384
- conn . killCursors ( ns , cursorState , ( err , result ) => {
385
- if ( err && isSDAMUnrecoverableError ( err ) ) {
386
- this . emit ( 'error' , err ) ;
387
- }
388
-
389
- cb ( err , result ) ;
390
- } ) ;
344
+ conn . killCursors ( ns , cursorState , makeOperationHandler ( this , null , cb ) ) ;
391
345
} , callback ) ;
392
346
}
393
347
@@ -482,21 +436,26 @@ function executeWriteOperation(args, options, callback) {
482
436
483
437
server . s . pool . withConnection ( ( err , conn , cb ) => {
484
438
if ( err ) return cb ( err ) ;
439
+ conn [ op ] ( ns , ops , options , makeOperationHandler ( server , options , cb ) ) ;
440
+ } , callback ) ;
441
+ }
485
442
486
- conn [ op ] ( ns , ops , options , ( err , result ) => {
487
- if ( err ) {
488
- if ( options . session && err instanceof MongoNetworkError ) {
443
+ function makeOperationHandler ( server , options , callback ) {
444
+ return function handleOperationResult ( err , result ) {
445
+ if ( err ) {
446
+ if ( err instanceof MongoNetworkError ) {
447
+ if ( options && options . session ) {
489
448
options . session . serverSession . isDirty = true ;
490
449
}
450
+ }
491
451
492
- if ( isSDAMUnrecoverableError ( err ) ) {
493
- server . emit ( 'error' , err ) ;
494
- }
452
+ if ( isSDAMUnrecoverableError ( err ) ) {
453
+ server . emit ( 'error' , err ) ;
495
454
}
455
+ }
496
456
497
- cb ( err , result ) ;
498
- } ) ;
499
- } , callback ) ;
457
+ callback ( err , result ) ;
458
+ } ;
500
459
}
501
460
502
461
module . exports = {
0 commit comments