Skip to content

Commit

Permalink
refactor(server): make common operation result handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Jan 24, 2020
1 parent b740940 commit 3efa4c7
Showing 1 changed file with 18 additions and 59 deletions.
77 changes: 18 additions & 59 deletions lib/core/sdam/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,7 @@ class Server extends EventEmitter {

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);

conn.command(ns, cmd, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}

if (isSDAMUnrecoverableError(err)) {
this.emit('error', err);
}
}

cb(err, result);
});
conn.command(ns, cmd, options, makeOperationHandler(this, options, cb));
}, callback);
}

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

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);

conn.query(ns, cmd, cursorState, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}

if (isSDAMUnrecoverableError(err)) {
this.emit('error', err);
}
}

cb(err, result);
});
conn.query(ns, cmd, cursorState, options, makeOperationHandler(this, options, cb));
}, callback);
}

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

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);

conn.getMore(ns, cursorState, batchSize, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
options.session.serverSession.isDirty = true;
}

if (isSDAMUnrecoverableError(err)) {
this.emit('error', err);
}
}

cb(err, result);
});
conn.getMore(ns, cursorState, batchSize, options, makeOperationHandler(this, options, cb));
}, callback);
}

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

this.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);

conn.killCursors(ns, cursorState, (err, result) => {
if (err && isSDAMUnrecoverableError(err)) {
this.emit('error', err);
}

cb(err, result);
});
conn.killCursors(ns, cursorState, makeOperationHandler(this, null, cb));
}, callback);
}

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

server.s.pool.withConnection((err, conn, cb) => {
if (err) return cb(err);
conn[op](ns, ops, options, makeOperationHandler(server, options, cb));
}, callback);
}

conn[op](ns, ops, options, (err, result) => {
if (err) {
if (options.session && err instanceof MongoNetworkError) {
function makeOperationHandler(server, options, callback) {
return function handleOperationResult(err, result) {
if (err) {
if (err instanceof MongoNetworkError) {
if (options && options.session) {
options.session.serverSession.isDirty = true;
}
}

if (isSDAMUnrecoverableError(err)) {
server.emit('error', err);
}
if (isSDAMUnrecoverableError(err)) {
server.emit('error', err);
}
}

cb(err, result);
});
}, callback);
callback(err, result);
};
}

module.exports = {
Expand Down

0 comments on commit 3efa4c7

Please sign in to comment.