d81bc91bd2
refs #7116, refs #2001 - Changes the way Ghost errors are implemented to benefit from proper inheritance - Moves all error definitions into a single file - Changes the error constructor to take an options object, rather than needing the arguments to be passed in the correct order. - Provides a wrapper so that any errors that haven't already been converted to GhostErrors get converted before they are displayed. Summary of changes: * 🐛 set NODE_ENV in config handler * ✨ add GhostError implementation (core/server/errors.js) - register all errors in one file - inheritance from GhostError - option pattern * 🔥 remove all error files * ✨ wrap all errors into GhostError in case of HTTP * 🎨 adaptions - option pattern for errors - use GhostError when needed * 🎨 revert debug deletion and add TODO for error id's
73 lines
2.7 KiB
JavaScript
73 lines
2.7 KiB
JavaScript
var _ = require('lodash'),
|
|
Promise = require('bluebird'),
|
|
SchedulingBase = require(__dirname + '/SchedulingBase'),
|
|
errors = require(__dirname + '/../errors');
|
|
|
|
exports.createAdapter = function (options) {
|
|
options = options || {};
|
|
|
|
var adapter = null,
|
|
activeAdapter = options.active,
|
|
internalPath = options.internalPath,
|
|
contentPath = options.contentPath;
|
|
|
|
if (!activeAdapter) {
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Please provide an active adapter.'}));
|
|
}
|
|
|
|
/**
|
|
* CASE: active adapter is a npm module
|
|
*/
|
|
try {
|
|
adapter = new (require(activeAdapter))(options);
|
|
} catch (err) {
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* CASE: active adapter is located in specific ghost path
|
|
*/
|
|
try {
|
|
adapter = adapter || new (require(contentPath + activeAdapter))(options);
|
|
} catch (err) {
|
|
// CASE: only throw error if module does exist
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
|
}
|
|
// CASE: if module not found it can be an error within the adapter (cannot find bluebird for example)
|
|
else if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(contentPath + activeAdapter) === -1) {
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* CASE: active adapter is located in internal ghost path
|
|
*/
|
|
try {
|
|
adapter = adapter || new (require(internalPath + activeAdapter))(options);
|
|
} catch (err) {
|
|
// CASE: only throw error if module does exist
|
|
if (err.code === 'MODULE_NOT_FOUND') {
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'We cannot find your adapter in: ' + contentPath + ' or: ' + internalPath}));
|
|
}
|
|
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
|
}
|
|
|
|
if (!(adapter instanceof SchedulingBase)) {
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not inherit from the SchedulingBase.'}));
|
|
}
|
|
|
|
if (!adapter.requiredFns) {
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not provide the minimum required functions.'}));
|
|
}
|
|
|
|
if (_.xor(adapter.requiredFns, Object.keys(_.pick(Object.getPrototypeOf(adapter), adapter.requiredFns))).length) {
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not provide the minimum required functions.'}));
|
|
}
|
|
|
|
return Promise.resolve(adapter);
|
|
};
|