From 357b64d70431ec5ca721eb45a63b082c18e6ffa3 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Sun, 6 Apr 2014 11:53:47 -0500 Subject: [PATCH] Remove query emit 'end' event when query has error Closes #547 --- lib/query.js | 6 ++---- test/integration/client/query-error-handling-tests.js | 8 +++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/query.js b/lib/query.js index 3dd752057..ea9459426 100644 --- a/lib/query.js +++ b/lib/query.js @@ -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) { diff --git a/test/integration/client/query-error-handling-tests.js b/test/integration/client/query-error-handling-tests.js index 8ac060eac..030cd4930 100644 --- a/test/integration/client/query-error-handling-tests.js +++ b/test/integration/client/query-error-handling-tests.js @@ -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";