From b7f6d0d673a58d1a1cdc4634d7f9d97aea6ddf6a Mon Sep 17 00:00:00 2001 From: samudurand Date: Fri, 25 Nov 2016 21:36:54 +0000 Subject: [PATCH 1/4] handle sequelize errors --- lib/index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8865f6b..10c3306 100644 --- a/lib/index.js +++ b/lib/index.js @@ -32,8 +32,11 @@ internals.configure = function (opts) { return opts.sequelize.authenticate() .then(() => { const files = Models.getFiles(opts.models); - const models = Models.applyRelations(Models.load(files, opts.sequelize.import.bind(opts.sequelize))); - return models; + return Models.applyRelations(Models.load(files, opts.sequelize.import.bind(opts.sequelize))); + }, (err) => { + return Promise.reject( + new Error("An error occured while attempting to connect to DB [" + opts.name + "], please check the configuration.") + ); }) .then((models) => { if (opts.sync) { @@ -45,7 +48,7 @@ internals.configure = function (opts) { .then((database) => { if (opts.onConnect) { let maybePromise = opts.onConnect(opts.sequelize); - if (maybePromise && typeof maybePromise.then === 'function') + if (maybePromise && typeof maybePromise.then === 'function') return maybePromise.then(() => database); } return database; @@ -77,6 +80,8 @@ exports.register = function(server, options, next) { .then((db) => { server.expose(opts.name, db); return Promise.resolve(db); + }, (err) => { + return Promise.reject(err) }) ]); }, []); @@ -84,6 +89,8 @@ exports.register = function(server, options, next) { Promise.all(configured) .then(() => { return next() + },(err) => { + return next(err) }) .catch((err) => { return next(err) From 2b3002f67044996fd619ba00c59bb53cb9ee32f8 Mon Sep 17 00:00:00 2001 From: samudurand Date: Fri, 25 Nov 2016 23:25:18 +0000 Subject: [PATCH 2/4] add a connection error test --- lib/index.js | 2 +- test/index.js | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 10c3306..e987546 100644 --- a/lib/index.js +++ b/lib/index.js @@ -35,7 +35,7 @@ internals.configure = function (opts) { return Models.applyRelations(Models.load(files, opts.sequelize.import.bind(opts.sequelize))); }, (err) => { return Promise.reject( - new Error("An error occured while attempting to connect to DB [" + opts.name + "], please check the configuration.") + new Error("An error occurred while attempting to connect to DB [" + opts.name + "], please check the configuration. Details : " + err.message) ); }) .then((models) => { diff --git a/test/index.js b/test/index.js index ed44d87..524aa8a 100644 --- a/test/index.js +++ b/test/index.js @@ -30,7 +30,7 @@ lab.suite('hapi-sequelize', () => { const onConnect = function (database) { server.log('onConnect called'); - } + }; const spy = Sinon.spy(onConnect); @@ -59,6 +59,44 @@ lab.suite('hapi-sequelize', () => { }) }); + test('plugin fails to register when Sequelize fails to connect', { parallel: true }, (done) => { + + const server = new Hapi.Server(); + server.connection(); + + const onConnect = function (database) { + server.log('onConnect called'); + }; + + const spy = Sinon.spy(onConnect); + + const sequelize = new Sequelize('shop', 'root', '', { + host: '127.0.0.1', + port: 3307, + dialect: 'mysql' + }); + + server.register([ + { + register: require('../lib'), + options: [ + { + name: 'shop', + models: ['./test/models/**/*.js'], + sequelize: sequelize, + sync: true, + forceSync: true, + onConnect: spy + } + ] + } + ], (err) => { + expect(err).to.exist(); + expect(err.message).to.include('ECONNREFUSED'); + done(); + }) + }); + test('plugin throws error when no models are found', { parallel: true }, (done) => { const server = new Hapi.Server(); From a839d63afb24b566297899c5c92255b306d8cb18 Mon Sep 17 00:00:00 2001 From: samudurand Date: Fri, 25 Nov 2016 23:31:43 +0000 Subject: [PATCH 3/4] fix eslint errors --- test/index.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/index.js b/test/index.js index 524aa8a..23ce005 100644 --- a/test/index.js +++ b/test/index.js @@ -7,9 +7,6 @@ const Sinon = require('sinon'); const Hapi = require('hapi'); const Sequelize = require('sequelize'); -// Module globals -const internals = {}; - // Test shortcuts const lab = exports.lab = Lab.script(); const test = lab.test; @@ -28,7 +25,7 @@ lab.suite('hapi-sequelize', () => { dialect: 'mysql' }); - const onConnect = function (database) { + const onConnect = function () { server.log('onConnect called'); }; @@ -64,7 +61,7 @@ lab.suite('hapi-sequelize', () => { const server = new Hapi.Server(); server.connection(); - const onConnect = function (database) { + const onConnect = function () { server.log('onConnect called'); }; From 470890eaf6dda3105df82d296d71636ab095c615 Mon Sep 17 00:00:00 2001 From: samudurand Date: Sat, 26 Nov 2016 10:04:48 +0000 Subject: [PATCH 4/4] fix tests --- test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 23ce005..630f718 100644 --- a/test/index.js +++ b/test/index.js @@ -25,7 +25,7 @@ lab.suite('hapi-sequelize', () => { dialect: 'mysql' }); - const onConnect = function () { + const onConnect = function (database) { server.log('onConnect called'); }; @@ -61,7 +61,7 @@ lab.suite('hapi-sequelize', () => { const server = new Hapi.Server(); server.connection(); - const onConnect = function () { + const onConnect = function (database) { server.log('onConnect called'); };