Skip to content

Commit

Permalink
Always raise 'error' event for non-query errors
Browse files Browse the repository at this point in the history
For brianc/node-postgres#1503. Not backwards-compatible (semver major).
  • Loading branch information
charmander authored and brianc committed May 4, 2018
1 parent 25d12eb commit 8fecf7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
13 changes: 2 additions & 11 deletions index.js
Expand Up @@ -36,7 +36,6 @@ var Client = module.exports = function (config) {
this._startReading()
})

this.on('_queryError', this._onQueryError.bind(this))
this.on('result', this._onResult.bind(this))
this.on('readyForQuery', this._onReadyForQuery.bind(this))
}
Expand Down Expand Up @@ -153,11 +152,7 @@ Client.prototype.end = function (cb) {

Client.prototype._readError = function (message) {
var err = new Error(message || this.pq.errorMessage())
if (this._queryCallback) {
this.emit('_queryError', err)
} else {
this.emit('error', err)
}
this.emit('error', err)
}

Client.prototype._stopReading = function () {
Expand All @@ -175,7 +170,7 @@ Client.prototype._emitResult = function (pq) {
var status = pq.resultStatus()
switch (status) {
case 'PGRES_FATAL_ERROR':
this._readError()
this._queryError = new Error(this.pq.resultErrorMessage())
break

case 'PGRES_TUPLES_OK':
Expand Down Expand Up @@ -291,10 +286,6 @@ Client.prototype._dispatchQuery = function (pq, fn, cb) {
this._waitForDrain(pq, cb)
}

Client.prototype._onQueryError = function (err) {
this._queryError = err
}

Client.prototype._onResult = function (result) {
if (this._resultCount === 0) {
this._results = result
Expand Down
18 changes: 18 additions & 0 deletions test/connection-errors.js
@@ -0,0 +1,18 @@
'use strict'

var Client = require('../')
var assert = require('assert')

describe('connection errors', function () {
it('raise error events', function (done) {
var client = new Client()
client.connectSync()
client.query('SELECT pg_terminate_backend(pg_backend_pid())', assert.fail)
client.on('error', function (err) {
assert(/^server closed the connection unexpectedly/.test(err.message))
assert.strictEqual(client.pq.resultErrorFields().sqlState, '57P01')
client.end()
done()
})
})
})

0 comments on commit 8fecf7a

Please sign in to comment.