0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

🎨 optimise sephiroth error handling (#7531)

refs #7489
This commit is contained in:
Katharina Irrgang 2016-10-10 19:31:32 +02:00 committed by Hannah Wolfe
parent f570aaef3c
commit 8d9414e8ba
5 changed files with 78 additions and 32 deletions

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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') {

View file

@ -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;

View file

@ -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
});
});
};