Skip to content

Commit 3efa4c7

Browse files
committed
refactor(server): make common operation result handler
1 parent b740940 commit 3efa4c7

File tree

1 file changed

+18
-59
lines changed

1 file changed

+18
-59
lines changed

lib/core/sdam/server.js

+18-59
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,7 @@ class Server extends EventEmitter {
279279

280280
this.s.pool.withConnection((err, conn, cb) => {
281281
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));
296283
}, callback);
297284
}
298285

@@ -312,20 +299,7 @@ class Server extends EventEmitter {
312299

313300
this.s.pool.withConnection((err, conn, cb) => {
314301
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));
329303
}, callback);
330304
}
331305

@@ -345,20 +319,7 @@ class Server extends EventEmitter {
345319

346320
this.s.pool.withConnection((err, conn, cb) => {
347321
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));
362323
}, callback);
363324
}
364325

@@ -380,14 +341,7 @@ class Server extends EventEmitter {
380341

381342
this.s.pool.withConnection((err, conn, cb) => {
382343
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));
391345
}, callback);
392346
}
393347

@@ -482,21 +436,26 @@ function executeWriteOperation(args, options, callback) {
482436

483437
server.s.pool.withConnection((err, conn, cb) => {
484438
if (err) return cb(err);
439+
conn[op](ns, ops, options, makeOperationHandler(server, options, cb));
440+
}, callback);
441+
}
485442

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) {
489448
options.session.serverSession.isDirty = true;
490449
}
450+
}
491451

492-
if (isSDAMUnrecoverableError(err)) {
493-
server.emit('error', err);
494-
}
452+
if (isSDAMUnrecoverableError(err)) {
453+
server.emit('error', err);
495454
}
455+
}
496456

497-
cb(err, result);
498-
});
499-
}, callback);
457+
callback(err, result);
458+
};
500459
}
501460

502461
module.exports = {

0 commit comments

Comments
 (0)