From 045ff8b9f80faae85abcff58c4b3c4535ff6f822 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 23 Jan 2013 22:32:17 +0000 Subject: [PATCH] status command and some basic error storing in module load [#198] --- modules/admin/commands.js | 21 ++++++++++++++++++++- run.js | 15 ++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/modules/admin/commands.js b/modules/admin/commands.js index 7015dd2..6c8c674 100644 --- a/modules/admin/commands.js +++ b/modules/admin/commands.js @@ -102,6 +102,21 @@ var commands = function(dbot) { }.bind(this)); }, + + 'status': function(event) { + var moduleName = event.params[1]; + if(_.has(dbot.status, moduleName)) { + var status = dbot.status[moduleName]; + if(status === true) { + event.reply(moduleName + ' status: Shit looks good.'); + } else { + event.reply(moduleName + ' status: Failed to load: ' + status); + } + } else { + event.reply('Either that module wasn\'t on the roster or shit is totally fucked.'); + } + }, + // Reload DB, translations and modules. 'reload': function(event) { dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); @@ -125,7 +140,11 @@ var commands = function(dbot) { if(!_.include(dbot.config.moduleNames, moduleName)) { dbot.config.moduleNames.push(moduleName); dbot.reloadModules(); - event.reply(dbot.t('load_module', {'moduleName': moduleName})); + if(dbot.status[moduleName] === true) { + event.reply(dbot.t('load_module', {'moduleName': moduleName})); + } else { + event.reply('Failed to load ' + moduleName + '. See \'status ' + moduleName + '\'.'); + } } else { if(moduleName == 'web') { event.reply(dbot.t('already_loaded_web')); diff --git a/run.js b/run.js index e63ccc7..1807d04 100644 --- a/run.js +++ b/run.js @@ -5,7 +5,6 @@ var fs = require('fs'), require('./snippets'); var DBot = function(timers) { - // Load DB var rawDB; try { @@ -55,6 +54,7 @@ var DBot = function(timers) { // Initialise run-time resources this.usage = {}; + this.status = {}; this.sessionData = {}; this.timers = timers.create(); @@ -119,6 +119,7 @@ DBot.prototype.reloadModules = function() { this.rawModules = []; this.pages = {}; + this.status = {}; this.modules = {}; this.commands = {}; this.api = {}; @@ -151,6 +152,7 @@ DBot.prototype.reloadModules = function() { this.instance.removeListeners(); moduleNames.each(function(name) { + this.status[name] = true; var moduleDir = './modules/' + name + '/'; var cacheKey = require.resolve(moduleDir + name); delete require.cache[cacheKey]; @@ -172,7 +174,13 @@ DBot.prototype.reloadModules = function() { } try { - var defaultConfig = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8')); + var defaultConfig = fs.readFileSync(moduleDir + 'config.json', 'utf-8'); + try { + defaultConfig = JSON.parse(defaultConfig); + } catch(err) { // syntax error + this.status[name] = 'Error parsing config: ' + err + ' ' + err.stack.split('\n')[2].trim(); + return; + } config = _.defaults(config, defaultConfig); } catch(err) { // Invalid or no config data @@ -188,7 +196,7 @@ DBot.prototype.reloadModules = function() { }, [], this); if(unmetDependencies.length != 0) { - throw new Error("Dependencies not met: " + unmetDependencies); + this.status[name] = 'Dependencies not met: ' + unmetDependencies; return; } } @@ -265,6 +273,7 @@ DBot.prototype.reloadModules = function() { this.modules[module.name] = module; } catch(err) { console.log(this.t('module_load_error', {'moduleName': name})); + this.status[name] = err + ' - ' + err.stack.split('\n')[1].trim(); if(this.config.debugMode) { console.log('MODULE ERROR (' + name + '): ' + err.stack ); } else {