diff --git a/core/server/ghost-server.js b/core/server/ghost-server.js index 566c958bcc..082ea73118 100644 --- a/core/server/ghost-server.js +++ b/core/server/ghost-server.js @@ -7,7 +7,6 @@ var debug = require('debug')('ghost:server'), path = require('path'), _ = require('lodash'), errors = require('./errors'), - logging = require('./logging'), config = require('./config'), i18n = require('./i18n'), moment = require('moment'); @@ -46,7 +45,7 @@ GhostServer.prototype.start = function (externalApp) { permissions: '660' }; - return new Promise(function (resolve) { + return new Promise(function (resolve, reject) { if (config.get('server').hasOwnProperty('socket')) { socketConfig = config.get('server').socket; @@ -75,21 +74,23 @@ GhostServer.prototype.start = function (externalApp) { } self.httpServer.on('error', function (error) { + var ghostError; + if (error.errno === 'EADDRINUSE') { - logging.error(new errors.GhostError({ + ghostError = new errors.GhostError({ message: i18n.t('errors.httpServer.addressInUse.error'), context: i18n.t('errors.httpServer.addressInUse.context', {port: config.get('server').port}), help: i18n.t('errors.httpServer.addressInUse.help') - })); + }); } else { - logging.error(new errors.GhostError({ + ghostError = new errors.GhostError({ message: i18n.t('errors.httpServer.otherError.error', {errorNumber: error.errno}), context: i18n.t('errors.httpServer.otherError.context'), help: i18n.t('errors.httpServer.otherError.help') - })); + }); } - process.exit(-1); + reject(ghostError); }); self.httpServer.on('connection', self.connection.bind(self)); self.httpServer.on('listening', function () { diff --git a/index.js b/index.js index 7e63be21da..2ba6e4db53 100644 --- a/index.js +++ b/index.js @@ -15,8 +15,22 @@ ghost().then(function (ghostServer) { debug('Starting Ghost'); // Let Ghost handle starting our server instance. - ghostServer.start(parentApp); -}).catch(function (err) { - logging.error(new errors.GhostError({err: err})); - process.exit(0); + return ghostServer.start(parentApp).then(function afterStart() { + // if IPC messaging is enabled, ensure ghost sends message to parent + // process on successful start + if (process.send) { + process.send({started: true}); + } + }); +}).catch(function (error) { + if (!(error instanceof errors.GhostError)) { + error = new errors.GhostError({err: error}); + } + + if (process.send) { + process.send({started: false, error: error.message}); + } + + logging.error(error); + process.exit(-1); });