diff --git a/core/server/data/sephiroth/index.js b/core/server/data/sephiroth/index.js index 6817101ad1..f96b9bb594 100644 --- a/core/server/data/sephiroth/index.js +++ b/core/server/data/sephiroth/index.js @@ -5,10 +5,6 @@ function Sephiroth(options) { this.utils = require('./lib/utils'); this.database = require('./lib/database'); - if (!options.database) { - this.utils.throwError({code: this.utils.errors.databaseConfigMissing}); - } - this.database.connect(options.database); } diff --git a/core/server/data/sephiroth/lib/commands/init.js b/core/server/data/sephiroth/lib/commands/init.js index 6676c34537..5eddbd4da1 100644 --- a/core/server/data/sephiroth/lib/commands/init.js +++ b/core/server/data/sephiroth/lib/commands/init.js @@ -1,6 +1,7 @@ var Promise = require('bluebird'), path = require('path'), utils = require('../utils'), + errors = require('../errors'), logging = require('../../../../logging'); /** @@ -34,7 +35,7 @@ module.exports = function init(options) { type: 'init' }); }).catch(function (err) { - if (err.code === utils.errors.taskFound) { + if (err instanceof errors.MigrationExistsError) { logging.warn('Skipping:' + task.name); return Promise.resolve(); } diff --git a/core/server/data/sephiroth/lib/database.js b/core/server/data/sephiroth/lib/database.js index 93fe55bb31..07db0c7a2b 100644 --- a/core/server/data/sephiroth/lib/database.js +++ b/core/server/data/sephiroth/lib/database.js @@ -4,6 +4,7 @@ var knex = require('knex'); * we only support knex */ exports.connect = function connect(options) { + options = options || {}; var client = options.client; if (client === 'sqlite3') { diff --git a/core/server/data/sephiroth/lib/errors.js b/core/server/data/sephiroth/lib/errors.js new file mode 100644 index 0000000000..2de8766e98 --- /dev/null +++ b/core/server/data/sephiroth/lib/errors.js @@ -0,0 +1,59 @@ +var _ = require('lodash'), + util = require('util'); + +function SephirothError(options) { + options = options || {}; + var self = this; + + if (_.isString(options)) { + throw new Error('Please instantiate Errors with the option pattern. e.g. new errors.SephirothError({message: ...})'); + } + + Error.call(this); + Error.captureStackTrace(this, SephirothError); + + /** + * defaults + */ + this.statusCode = 500; + this.errorType = this.name = 'SephirothError'; + this.id = 0; + + /** + * option overrides + */ + this.id = options.id || this.id; + this.message = options.message || this.message; + this.code = options.code || this.code; + this.errorType = this.name = options.errorType || this.errorType; + + // error to inherit from, override! + if (options.err) { + Object.getOwnPropertyNames(options.err).forEach(function (property) { + self[property] = options.err[property] || self[property]; + }); + } +} + +// jscs:disable +var errors = { + MigrationExistsError: function MigrationExistsError(options) { + SephirothError.call(this, _.merge({ + id: 100, + errorType: 'MigrationExistsError' + }, options)); + }, + DatabaseIsNotOkError: function DatabaseIsNotOkError(options) { + SephirothError.call(this, _.merge({ + id: 200, + errorType: 'DatabaseIsNotOkError' + }, options)); + } +}; + +_.each(errors, function (error) { + util.inherits(error, SephirothError); +}); + +module.exports = errors; +module.exports.SephirothError = SephirothError; diff --git a/core/server/data/sephiroth/lib/utils.js b/core/server/data/sephiroth/lib/utils.js index a29e069636..414210adba 100644 --- a/core/server/data/sephiroth/lib/utils.js +++ b/core/server/data/sephiroth/lib/utils.js @@ -2,27 +2,9 @@ var path = require('path'), _ = require('lodash'), fs = require('fs'), database = require('./database'), + errors = require('./errors'), logging = require('../../../logging'); -exports.errors = { - taskFound: 100, - unknown: 99, - migrationsTableMissing: 98, - dbInitMissing: 97, - databaseConfigMissing: 96 -}; - -/** - * Sephiroth erorr handling for now - */ -exports.throwError = function throwError(options) { - var code = options.code, - err = new Error(); - - err.code = code; - throw err; -}; - exports.readTasks = function readTasks(absolutePath) { var files = [], tasks = []; @@ -39,7 +21,7 @@ exports.readTasks = function readTasks(absolutePath) { return tasks; } catch (err) { - throw err; + throw new errors.SephirothError({err: err}); } }; @@ -65,12 +47,12 @@ exports.preTask = function preTask(options) { } if (_.find(migrations, {name: task, type: type})) { - exports.throwError({code: exports.errors.taskFound}); + throw new errors.MigrationExistsError(); } }) .catch(function (err) { // CASE: table does not exist - if (err.errno === 1) { + if (err.errno === 1 || err.errno === 1146) { logging.info('Creating table: migrations'); return (localDatabase || database.knex).schema.createTable('migrations', function (table) { @@ -117,14 +99,21 @@ exports.isDatabaseOK = function isDatabaseOK(options) { return; } - exports.throwError({code: exports.errors.dbInitMissing}); + throw new errors.DatabaseIsNotOkError({ + message: 'Please run node core/server/data/sephiroth/bin/sephiroth init.', + code: 'DB_NOT_INITIALISED' + }); }) .catch(function (err) { - // CASE: table does not exist - if (err.errno === 1) { - exports.throwError({code: exports.errors.dbInitMissing}); + if (err.errno === 1 || err.errno === 1146) { + throw new errors.DatabaseIsNotOkError({ + message: 'Please run node core/server/data/sephiroth/bin/sephiroth init.', + code: 'MIGRATION_TABLE_IS_MISSING' + }); } - exports.throwError({code: exports.errors.unknown}); + throw new errors.SephirothError({ + err: err + }); }); };