Skip to content

Commit

Permalink
Remove query emit 'end' event when query has error
Browse files Browse the repository at this point in the history
Closes #547
  • Loading branch information
brianc committed Apr 6, 2014
1 parent c3f5c06 commit 357b64d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 2 additions & 4 deletions lib/query.js
Expand Up @@ -93,11 +93,9 @@ Query.prototype.handleError = function(err, connection) {
//if callback supplied do not emit error event as uncaught error
//events will bubble up to node process
if(this.callback) {
this.callback(err);
} else {
this.emit('error', err);
return this.callback(err);
}
this.emit('end');
this.emit('error', err);
};

Query.prototype.submit = function(connection) {
Expand Down
8 changes: 7 additions & 1 deletion test/integration/client/query-error-handling-tests.js
Expand Up @@ -12,10 +12,16 @@ test('error during query execution', function() {
pidColName = 'pid';
queryColName = 'query';
}
client.query(sleepQuery, assert.calls(function(err, result) {
var query1 = client.query(sleepQuery, assert.calls(function(err, result) {
assert(err);
client.end();
}));
//ensure query1 does not emit an 'end' event
//because it was killed and received an error
//https://github.com/brianc/node-postgres/issues/547
query1.on('end', function() {
assert.fail('Client with an error should not emit "end" event')
})
var client2 = new Client(helper.args);
client2.connect(assert.success(function() {
var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1";
Expand Down

0 comments on commit 357b64d

Please sign in to comment.