Skip to content

Commit

Permalink
Handle operations that return a promise instead of an event emitter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TuckerWhitehouse authored Sep 8, 2020
1 parent 39e08f3 commit a8e29ef
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/mysql/lib/mysql_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function patchOf(poolCluster) {

var resultPool = poolCluster[baseFcn].apply(poolCluster, args);
return patchObject(resultPool);
}
};
}

function patchGetConnection(pool) {
Expand All @@ -105,13 +105,13 @@ function patchGetConnection(pool) {
args[args.length-1] = (err, connection) => {
if(connection) patchObject(connection);
return callback(err, connection);
}
};
}

var result = pool[baseFcn].apply(pool, args);
if (result && result.then instanceof Function) return result.then(patchObject);
else return result;
}
};
}

function patchObject(connection) {
Expand Down Expand Up @@ -208,10 +208,6 @@ function captureOperation(name) {
command = originalOperation.call(this, sql, args.values, args.callback);

if (!args.callback) {
command.on('end', function() {
subsegment.close();
});

var errorCapturer = function (err) {
subsegment.close(err);

Expand All @@ -222,14 +218,26 @@ function captureOperation(name) {
}
};

command.on(events.errorMonitor || 'error', errorCapturer);
if (command.then instanceof Function) {
command.then(() => {
subsegment.close();
});

command.catch(errorCapturer);
} else {
command.on('end', function() {
subsegment.close();
});

command.on(events.errorMonitor || 'error', errorCapturer);
}
}

subsegment.addSqlData(createSqlData(config, command));
subsegment.namespace = 'remote';

return command;
}
};
}

function createSqlData(config, command) {
Expand Down

0 comments on commit a8e29ef

Please sign in to comment.