Skip to content

Commit 357b64d

Browse files
committed
Remove query emit 'end' event when query has error
Closes #547
1 parent c3f5c06 commit 357b64d

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/query.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ Query.prototype.handleError = function(err, connection) {
9393
//if callback supplied do not emit error event as uncaught error
9494
//events will bubble up to node process
9595
if(this.callback) {
96-
this.callback(err);
97-
} else {
98-
this.emit('error', err);
96+
return this.callback(err);
9997
}
100-
this.emit('end');
98+
this.emit('error', err);
10199
};
102100

103101
Query.prototype.submit = function(connection) {

test/integration/client/query-error-handling-tests.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ test('error during query execution', function() {
1212
pidColName = 'pid';
1313
queryColName = 'query';
1414
}
15-
client.query(sleepQuery, assert.calls(function(err, result) {
15+
var query1 = client.query(sleepQuery, assert.calls(function(err, result) {
1616
assert(err);
1717
client.end();
1818
}));
19+
//ensure query1 does not emit an 'end' event
20+
//because it was killed and received an error
21+
//https://github.com/brianc/node-postgres/issues/547
22+
query1.on('end', function() {
23+
assert.fail('Client with an error should not emit "end" event')
24+
})
1925
var client2 = new Client(helper.args);
2026
client2.connect(assert.success(function() {
2127
var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1";

0 commit comments

Comments
 (0)