mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
parent
f570aaef3c
commit
8d9414e8ba
5 changed files with 78 additions and 32 deletions
|
@ -5,10 +5,6 @@ function Sephiroth(options) {
|
||||||
this.utils = require('./lib/utils');
|
this.utils = require('./lib/utils');
|
||||||
this.database = require('./lib/database');
|
this.database = require('./lib/database');
|
||||||
|
|
||||||
if (!options.database) {
|
|
||||||
this.utils.throwError({code: this.utils.errors.databaseConfigMissing});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.database.connect(options.database);
|
this.database.connect(options.database);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
var Promise = require('bluebird'),
|
var Promise = require('bluebird'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
utils = require('../utils'),
|
utils = require('../utils'),
|
||||||
|
errors = require('../errors'),
|
||||||
logging = require('../../../../logging');
|
logging = require('../../../../logging');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +35,7 @@ module.exports = function init(options) {
|
||||||
type: 'init'
|
type: 'init'
|
||||||
});
|
});
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
if (err.code === utils.errors.taskFound) {
|
if (err instanceof errors.MigrationExistsError) {
|
||||||
logging.warn('Skipping:' + task.name);
|
logging.warn('Skipping:' + task.name);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ var knex = require('knex');
|
||||||
* we only support knex
|
* we only support knex
|
||||||
*/
|
*/
|
||||||
exports.connect = function connect(options) {
|
exports.connect = function connect(options) {
|
||||||
|
options = options || {};
|
||||||
var client = options.client;
|
var client = options.client;
|
||||||
|
|
||||||
if (client === 'sqlite3') {
|
if (client === 'sqlite3') {
|
||||||
|
|
59
core/server/data/sephiroth/lib/errors.js
Normal file
59
core/server/data/sephiroth/lib/errors.js
Normal 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;
|
|
@ -2,27 +2,9 @@ var path = require('path'),
|
||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
database = require('./database'),
|
database = require('./database'),
|
||||||
|
errors = require('./errors'),
|
||||||
logging = require('../../../logging');
|
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) {
|
exports.readTasks = function readTasks(absolutePath) {
|
||||||
var files = [],
|
var files = [],
|
||||||
tasks = [];
|
tasks = [];
|
||||||
|
@ -39,7 +21,7 @@ exports.readTasks = function readTasks(absolutePath) {
|
||||||
|
|
||||||
return tasks;
|
return tasks;
|
||||||
} catch (err) {
|
} 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})) {
|
if (_.find(migrations, {name: task, type: type})) {
|
||||||
exports.throwError({code: exports.errors.taskFound});
|
throw new errors.MigrationExistsError();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function (err) {
|
.catch(function (err) {
|
||||||
// CASE: table does not exist
|
// CASE: table does not exist
|
||||||
if (err.errno === 1) {
|
if (err.errno === 1 || err.errno === 1146) {
|
||||||
logging.info('Creating table: migrations');
|
logging.info('Creating table: migrations');
|
||||||
|
|
||||||
return (localDatabase || database.knex).schema.createTable('migrations', function (table) {
|
return (localDatabase || database.knex).schema.createTable('migrations', function (table) {
|
||||||
|
@ -117,14 +99,21 @@ exports.isDatabaseOK = function isDatabaseOK(options) {
|
||||||
return;
|
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) {
|
.catch(function (err) {
|
||||||
// CASE: table does not exist
|
if (err.errno === 1 || err.errno === 1146) {
|
||||||
if (err.errno === 1) {
|
throw new errors.DatabaseIsNotOkError({
|
||||||
exports.throwError({code: exports.errors.dbInitMissing});
|
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
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue