mirror of
https://github.com/brianc/node-postgres.git
synced 2026-02-01 16:47:23 +00:00
When connection fail, emit the error. (#28)
* When connection fail, emit the error. If client connect failed, emit the connection error rather than swallowing it. * Add test for connection error.
This commit is contained in:
parent
cf28f9357f
commit
6a7edabc22
1
index.js
1
index.js
@ -43,6 +43,7 @@ Pool.prototype._create = function (cb) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
this.log('client connection error:', err)
|
this.log('client connection error:', err)
|
||||||
cb(err)
|
cb(err)
|
||||||
|
this.emit('error', err)
|
||||||
} else {
|
} else {
|
||||||
this.log('client connected')
|
this.log('client connected')
|
||||||
this.emit('connect', client)
|
this.emit('connect', client)
|
||||||
|
|||||||
@ -22,7 +22,7 @@ describe('events', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('emits "connect" only with a successful connection', function (done) {
|
it('emits "error" with a failed connection', function (done) {
|
||||||
var pool = new Pool({
|
var pool = new Pool({
|
||||||
// This client will always fail to connect
|
// This client will always fail to connect
|
||||||
Client: mockClient({
|
Client: mockClient({
|
||||||
@ -34,7 +34,29 @@ describe('events', function () {
|
|||||||
pool.on('connect', function () {
|
pool.on('connect', function () {
|
||||||
throw new Error('should never get here')
|
throw new Error('should never get here')
|
||||||
})
|
})
|
||||||
pool._create(function (err) {
|
pool.on('error', function (err) {
|
||||||
|
if (err) done()
|
||||||
|
else done(new Error('expected failure'))
|
||||||
|
})
|
||||||
|
pool.connect()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('callback err with a failed connection', function (done) {
|
||||||
|
var pool = new Pool({
|
||||||
|
// This client will always fail to connect
|
||||||
|
Client: mockClient({
|
||||||
|
connect: function (cb) {
|
||||||
|
process.nextTick(function () { cb(new Error('bad news')) })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
pool.on('connect', function () {
|
||||||
|
throw new Error('should never get here')
|
||||||
|
})
|
||||||
|
pool.on('error', function (err) {
|
||||||
|
if (!err) done(new Error('expected failure'))
|
||||||
|
})
|
||||||
|
pool.connect(function (err) {
|
||||||
if (err) done()
|
if (err) done()
|
||||||
else done(new Error('expected failure'))
|
else done(new Error('expected failure'))
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user