Skip to content

Commit

Permalink
fix(server): fixing un-handled promise rejections when initaitlizing …
Browse files Browse the repository at this point in the history
…the app
  • Loading branch information
lirantal committed Dec 28, 2016
1 parent f7acd13 commit f73ec2b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
62 changes: 47 additions & 15 deletions server/config/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ var config = require('../config'),
// Establish an SQL server connection, instantiating all models and schemas
function startSequelize() {
return new Promise(function (resolve, reject) {
let orm = null;
let orm = {};
if (config.orm) {
orm = require('./sequelize');
orm.sync()
.then(function () {
resolve(orm);
});
try {
orm = require('./sequelize');
orm.sync().then(function () {
resolve(orm);
});
} catch (e) {
reject(e);
}
} else {
resolve();
}
});
}
Expand All @@ -31,6 +36,9 @@ function startMongoose() {
.then(function(dbConnection) {
resolve(dbConnection);
})
.catch(function(e) {
reject(e);
});
});
}

Expand All @@ -41,10 +49,17 @@ function startMongoose() {
*/
function startExpress(db, orm) {
return new Promise(function (resolve, reject) {

// Initialize the ExpressJS web application server
const app = express.init(db, orm);
resolve(app);
})
let app;
try {
app = express.init(db, orm);
} catch(e) {
return reject(e);
}

return resolve(app);
});
}


Expand All @@ -54,11 +69,22 @@ function startExpress(db, orm) {
*/
function bootstrap () {
return new Promise(async function (resolve, reject) {
const orm = await startSequelize();
const db = await startMongoose();
const app = await startExpress(db, orm);
let orm, db, app;

resolve({
try {
orm = await startSequelize();
} catch (e) {
orm = {};
}

try {
db = await startMongoose();
app = await startExpress(db, orm);
} catch (e) {
return reject(new Error('unable to initialize Mongoose or ExpressJS'));
}

return resolve({
db: db,
orm: orm,
app: app
Expand All @@ -75,7 +101,13 @@ exports.bootstrap = bootstrap;
exports.start = function start() {
return new Promise(async function (resolve, reject) {

const { db, orm, app } = await bootstrap();
let db, orm, app;

try {
({db, orm, app} = await bootstrap());
} catch (e) {
return reject(e);
}

// Start the app by listening on <port> at <host>
app.listen(config.port, config.host, function () {
Expand All @@ -96,7 +128,7 @@ exports.start = function start() {

console.log('--');

resolve({
return resolve({
db: db,
orm: orm,
app: app
Expand Down
7 changes: 6 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
/**
* Module dependencies.
*/

var app = require('./config/lib/app');
app.start();

app.start().catch(function (e) {
console.log('server failed: ' + e.message);
throw (e);
});

0 comments on commit f73ec2b

Please sign in to comment.