不要怂,就是干,撸起袖子干!

Commit 93063a21 by Brian Johnson

allow for trapping postgres connection errors

connect() returns an event emitter which can be used to handle errors
gracefully rather than throwing an error in callback which would
terminate the entire node process
1 parent 5fa778ea
Showing with 4 additions and 1 deletions
...@@ -46,6 +46,7 @@ module.exports = (function() { ...@@ -46,6 +46,7 @@ module.exports = (function() {
ConnectorManager.prototype.connect = function() { ConnectorManager.prototype.connect = function() {
var self = this var self = this
var emitter = new (require('events').EventEmitter)()
// in case database is slow to connect, prevent orphaning the client // in case database is slow to connect, prevent orphaning the client
if (this.isConnecting) return if (this.isConnecting) return
...@@ -58,7 +59,7 @@ module.exports = (function() { ...@@ -58,7 +59,7 @@ module.exports = (function() {
self.isConnecting = false self.isConnecting = false
if (!!err) { if (!!err) {
throw err emitter.emit('error', err)
} else if (client) { } else if (client) {
client.query("SET TIME ZONE 'UTC'") client.query("SET TIME ZONE 'UTC'")
.on('end', function() { .on('end', function() {
...@@ -78,6 +79,8 @@ module.exports = (function() { ...@@ -78,6 +79,8 @@ module.exports = (function() {
this.client = new this.pg.Client(uri) this.client = new this.pg.Client(uri)
this.client.connect(connectCallback) this.client.connect(connectCallback)
} }
return emitter
} }
ConnectorManager.prototype.disconnect = function() { ConnectorManager.prototype.disconnect = function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!