Skip to content

Commit

Permalink
馃帹 optimise sephiroth error handling (#7531)
Browse files Browse the repository at this point in the history
refs #7489
  • Loading branch information
kirrg001 authored and ErisDS committed Oct 10, 2016
1 parent f570aae commit 8d9414e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 32 deletions.
4 changes: 0 additions & 4 deletions core/server/data/sephiroth/index.js
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion 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');

/**
Expand Down Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions core/server/data/sephiroth/lib/database.js
Expand Up @@ -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') {
Expand Down
59 changes: 59 additions & 0 deletions 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;
43 changes: 16 additions & 27 deletions core/server/data/sephiroth/lib/utils.js
Expand Up @@ -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 = [];
Expand All @@ -39,7 +21,7 @@ exports.readTasks = function readTasks(absolutePath) {

return tasks;
} catch (err) {
throw err;
throw new errors.SephirothError({err: err});
}
};

Expand All @@ -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) {
Expand Down Expand Up @@ -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
});
});
};

0 comments on commit 8d9414e

Please sign in to comment.