From c19d201e59d8dc07c3b5aa0d268d906fd0c5dece Mon Sep 17 00:00:00 2001 From: William F Wheeler II Date: Fri, 26 Jan 2018 22:15:03 -0500 Subject: [PATCH] fix: unhandled error event on connection Signed-off-by: William F Wheeler II --- index.js | 8 ++++++-- test/pg_test.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5e36513..1c8e2c9 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,6 @@ var PgDriver = Base.extend({ this.internals = intern; this.connection = connection; this.schema = schema || "public"; - this.connection.connect(); }, startMigration: function(cb){ @@ -517,7 +516,12 @@ exports.connect = function(config, intern, callback) { if (config.native) { pg = pg.native; } var db = config.db || new pg.Client(config); - callback(null, new PgDriver(db, config.schema, intern)); + db.connect(function(err) { + if(err) { + callback(err); + } + callback(null, new PgDriver(db, config.schema, intern)); + }); }; exports.base = PgDriver; diff --git a/test/pg_test.js b/test/pg_test.js index 54e3fd5..3862f6f 100644 --- a/test/pg_test.js +++ b/test/pg_test.js @@ -34,6 +34,16 @@ vows.describe('pg').addBatch({ db = _db; } } +}).addBatch({ + 'connect error': { + topic: function () { + driver.connect({host: 'fakehost'}, internals, this.callback); + }, + + 'shows connection error': function (err, _db) { + assert.isNotNull(err); + } + } }).addBatch({ 'createTable': { topic: function() {