feat: 添加异常拦截和打印

This commit is contained in:
coding
2017-10-20 09:14:42 +00:00
parent e5f212fd6b
commit 1058677c5f
4 changed files with 55 additions and 29 deletions

View File

@ -27,6 +27,12 @@ var log = base.getLog().static;
this.debug = function () { this.debug = function () {
log.d(this.name + Array.prototype.join.call(arguments, ' ')); log.d(this.name + Array.prototype.join.call(arguments, ' '));
} }
this.ex = function (ex) {
log.console('§4' + ex);
ex.getStackTrace().forEach(function (stack) {
log.console(' §e位于 §c%s §4行%s', stack.fileName, stack.lineNumber);
});
}
} }
global.Console = Console; global.Console = Console;
global.console = new Console(); global.console = new Console();

View File

@ -62,15 +62,25 @@ function on(jsp, name, exec) {
log.d('插件 %s 设置命令 %s(%s) 执行器 ...', jsp.description.name, name, c); log.d('插件 %s 设置命令 %s(%s) 执行器 ...', jsp.description.name, name, c);
if (exec.cmd) { if (exec.cmd) {
c.setExecutor(function (sender, cmd, command, args) { c.setExecutor(function (sender, cmd, command, args) {
try {
return exec.cmd(sender, command, args); return exec.cmd(sender, command, args);
} catch (ex) {
log.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s', sender.name, jsp.description.name, command, Java.from(args).join(' '), ex);
console.ex(ex);
}
}); });
} }
if (exec.tab) { if (exec.tab) {
c.setTabCompleter(function (sender, cmd, command, args) { c.setTabCompleter(function (sender, cmd, command, args) {
try {
var completions = new ArrayList(); var completions = new ArrayList();
var token = args[args.length - 1]; var token = args[args.length - 1];
StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions); StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions);
return completions; return completions;
} catch (ex) {
log.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s',sender.name ,jsp.description.name, command, Java.from(args).join(' '), ex);
console.ex(ex);
}
}); });
} }
} }

View File

@ -114,7 +114,12 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
EventPriority[priority], EventPriority[priority],
new EventExecutor({ new EventExecutor({
execute: function (listener, event) { execute: function (listener, event) {
try{
exec(event); exec(event);
} catch (ex){
log.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s', name, event.class.simpleName, ex);
console.ex(ex);
}
} }
}), }),
plugin, plugin,

View File

@ -76,7 +76,6 @@ function loadPlugin(file) {
} }
var plugin = require(file, { var plugin = require(file, {
cache: false, cache: false,
// 给插件注入单独的 console
hook: function (origin) { hook: function (origin) {
return beforeLoadHook(origin); return beforeLoadHook(origin);
} }
@ -99,8 +98,10 @@ function beforeLoadHook(origin) {
var result = origin; var result = origin;
// 处理 event 为了不影响 正常逻辑 event 还是手动require吧 // 处理 event 为了不影响 正常逻辑 event 还是手动require吧
// result = result + 'var event = {}; module.exports.event = event;'; // result = result + 'var event = {}; module.exports.event = event;';
// 注入 console 对象 // 注入 console 对象 // 给插件注入单独的 console
result = result + 'var console = new Console(); module.exports.console = console;'; result = result + 'var console = new Console(); module.exports.console = console;';
// 插件注入 self 对象
result = result + 'var self = {}; module.exports.self = self;';
return result; return result;
} }
@ -108,6 +109,10 @@ function afterLoadHook(plugin) {
// plugin.event.on = event.on.bind(plugin); // plugin.event.on = event.on.bind(plugin);
// 给 console 添加插件名称 // 给 console 添加插件名称
plugin.console.name = plugin.description.name; plugin.console.name = plugin.description.name;
// 赋值 self
for (var i in plugin){
plugin.self[i] = plugin[i];
}
} }
/** /**
@ -119,13 +124,11 @@ function initPlugin(file, plugin){
// 初始化 __DATA__ // 初始化 __DATA__
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name);
// 初始化 getDataFolder() // 初始化 getDataFolder()
plugin.getDataFolder = function() { plugin.getDataFolder = function() { return plugin.__DATA__; }
return plugin.__DATA__;
}
// 初始化 getFile() // 初始化 getFile()
plugin.getFile = function(name) { plugin.getFile = function(name) { return fs.file(plugin.getDataFolder(), name); }
return fs.file(plugin.getDataFolder(), name);
} // 初始化插件配置相关方法
initPluginConfig(plugin); initPluginConfig(plugin);
command.enable(plugin); command.enable(plugin);
@ -136,6 +139,8 @@ function initPlugin(file, plugin){
* 初始化插件配置 * 初始化插件配置
*/ */
function initPluginConfig(plugin){ function initPluginConfig(plugin){
// 初始化 config
plugin.configFile = plugin.getFile('config.yml');
/** /**
* 获取配置文件 * 获取配置文件
* @constructor * @constructor
@ -153,6 +158,14 @@ function initPluginConfig(plugin){
return yaml.safeLoad(base.read(file)); return yaml.safeLoad(base.read(file));
} }
} }
/**
* 重载配置文件
* @constructor
* @constructor (file|string)
*/
plugin.reloadConfig = function() {
plugin.config = plugin.getConfig(plugin.configFile);
}
/** /**
* 保存配置文件 * 保存配置文件
* @constructor * @constructor
@ -169,11 +182,9 @@ function initPluginConfig(plugin){
break; break;
} }
} }
// 初始化 config
plugin.configFile = plugin.getFile('config.yml');
if (plugin.configFile.isFile()) { if (plugin.configFile.isFile()) {
plugin.config = plugin.getConfig('config.yml'); plugin.config = plugin.getConfig('config.yml');
} else if ( plugin.description.config ){ } else if (plugin.description.config ){
plugin.config = plugin.description.config; plugin.config = plugin.description.config;
plugin.saveConfig(); plugin.saveConfig();
} }
@ -186,24 +197,18 @@ function runAndCatch(jsp, exec, ext) {
exec.bind(jsp)(); exec.bind(jsp)();
if (ext) { ext(); } if (ext) { ext(); }
} catch (ex) { } catch (ex) {
log.w('插件 %s 执行 %s 发生错误: %s', jsp.description.name, exec.name, ex.message); log.w('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s', jsp.description.name, exec.name, ex.message);
ex.printStackTrace(); console.ex(ex);
} }
} }
} }
function checkAndGet(args) { function checkAndGet(args) {
if (args.length === 0) { if (args.length === 0) { return plugins; }
return plugins;
}
var name = args[0]; var name = args[0];
// 如果是插件 则直接返回 // 如果是插件 则直接返回
if (name.description) { if (name.description) { return [name]; }
return [name]; if (!exports.plugins[name]) { throw new Error("插件 " + name + "不存在!"); }
}
if (!exports.plugins[name]) {
throw new Error("插件 " + name + "不存在!");
}
return [exports.plugins[name]]; return [exports.plugins[name]];
} }