Skip to content

Commit ea116c2

Browse files
committed
Always raise 'error' event for non-query errors
For brianc/node-postgres#1503. Not backwards-compatible (semver major).
1 parent 25d12eb commit ea116c2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

index.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var Client = module.exports = function (config) {
3636
this._startReading()
3737
})
3838

39-
this.on('_queryError', this._onQueryError.bind(this))
4039
this.on('result', this._onResult.bind(this))
4140
this.on('readyForQuery', this._onReadyForQuery.bind(this))
4241
}
@@ -153,11 +152,7 @@ Client.prototype.end = function (cb) {
153152

154153
Client.prototype._readError = function (message) {
155154
var err = new Error(message || this.pq.errorMessage())
156-
if (this._queryCallback) {
157-
this.emit('_queryError', err)
158-
} else {
159-
this.emit('error', err)
160-
}
155+
this.emit('error', err)
161156
}
162157

163158
Client.prototype._stopReading = function () {
@@ -175,7 +170,7 @@ Client.prototype._emitResult = function (pq) {
175170
var status = pq.resultStatus()
176171
switch (status) {
177172
case 'PGRES_FATAL_ERROR':
178-
this._readError()
173+
this._queryError = new Error(this.pq.resultErrorMessage())
179174
break
180175

181176
case 'PGRES_TUPLES_OK':
@@ -291,10 +286,6 @@ Client.prototype._dispatchQuery = function (pq, fn, cb) {
291286
this._waitForDrain(pq, cb)
292287
}
293288

294-
Client.prototype._onQueryError = function (err) {
295-
this._queryError = err
296-
}
297-
298289
Client.prototype._onResult = function (result) {
299290
if (this._resultCount === 0) {
300291
this._results = result

test/connection-errors.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
var Client = require('../')
4+
var assert = require('assert')
5+
6+
describe('connection errors', function () {
7+
it('raise error events', function (done) {
8+
var client = new Client()
9+
client.connectSync()
10+
client.query('SELECT pg_terminate_backend(pg_backend_pid())', assert.fail)
11+
client.on('error', function (err) {
12+
assert(/^server closed the connection unexpectedly/.test(err.message))
13+
assert.strictEqual(client.pq.resultErrorFields().sqlState, '57P01')
14+
client.end()
15+
done()
16+
})
17+
})
18+
})

0 commit comments

Comments
 (0)