Read activePlugins from settings & improve error handling

issue #769

- activePlugins were being read from settings in two different ways, this has been simplified
- error handling has been improved so that plugins do not crash Ghost
- used full error messaging capabilities to make it easier to recover from errors
This commit is contained in:
Hannah Wolfe 2013-10-29 11:24:58 +00:00
parent 507174a00b
commit 6a0a453a96
2 changed files with 22 additions and 23 deletions

View File

@ -353,34 +353,33 @@ Ghost.prototype.doFilter = function (name, args) {
// Initialise plugins. Will load from config.activePlugins by default
Ghost.prototype.initPlugins = function (pluginsToLoad) {
pluginsToLoad = pluginsToLoad || JSON.parse(this.settings('activePlugins'));
var self = this;
// If no activePlugins defined in config settings, look in database settings.
if (!_.isArray(pluginsToLoad)) {
// The value will be resolved in the promise
pluginsToLoad = models.Settings.read("activePlugins").then(function (activePluginsSetting) {
var settingValue = activePluginsSetting.get('value') || '[]';
try {
// We have to parse the value because it's a string
settingValue = JSON.parse(settingValue) || [];
} catch (e) {
return when.reject(new Error("Failed to parse activePlugins setting value: " + e.message));
}
// Resolve with the array value
return when.resolve(settingValue);
});
try {
// We have to parse the value because it's a string
pluginsToLoad = JSON.parse(this.settings('activePlugins')) || [];
} catch (e) {
errors.logError(
'Failed to parse activePlugins setting value: ' + e.message,
'Your plugins will not be loaded.',
'Check your settings table for typos in the activePlugins value. It should look like: ["plugin-1", "plugin2"] (double quotes required).'
);
return when.resolve();
}
}
return when(pluginsToLoad).then(function (pluginsToLoadValue) {
return plugins.init(self, pluginsToLoad).then(function (loadedPlugins) {
// Extend the loadedPlugins onto the available plugins
_.extend(self.availablePlugins, loadedPlugins);
});
}, errors.logAndThrowError);
return plugins.init(self, pluginsToLoad).then(function (loadedPlugins) {
// Extend the loadedPlugins onto the available plugins
_.extend(self.availablePlugins, loadedPlugins);
}).otherwise(function (err) {
errors.logError(
err.message || err,
'The plugin will not be loaded',
'Check with the plugin creator, or read the plugin documentation for more details on plugin requirements'
);
});
};
module.exports = Ghost;

View File

@ -471,4 +471,4 @@ when(ghost.init()).then(function () {
}
});
}, errors.logAndThrowError);
}).otherwise(errors.logAndThrowError);