From 915df476179484f2eb240b30a112e5fc1289fdc9 Mon Sep 17 00:00:00 2001 From: welefen Date: Mon, 4 Nov 2013 17:53:06 +0800 Subject: [PATCH] by welefen --- lib/Common/common.js | 9 ++++-- lib/Lib/Behavior/DenyIpBehavior.class.js | 4 +-- lib/Lib/Core/App.class.js | 8 ++--- lib/Lib/Core/Think.class.js | 37 +++++++++++------------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/Common/common.js b/lib/Common/common.js index 2daf8d06..6d6be765 100644 --- a/lib/Common/common.js +++ b/lib/Common/common.js @@ -194,14 +194,19 @@ global.ucfirst = function(name){ * @param {[type]} obj [description] * @return {[type]} [description] */ -global.throw_error = function(obj){ +global.throw_error = function(obj, http){ if (is_string(obj)) { obj = {msg: obj}; }; obj = extend({ type: "error", - msg: "" + msg: "", + http: http }, obj); + if (http) { + throw obj; + return; + }; throw new Error(JSON.stringify(obj)); } diff --git a/lib/Lib/Behavior/DenyIpBehavior.class.js b/lib/Lib/Behavior/DenyIpBehavior.class.js index 900adb99..4bb319a9 100644 --- a/lib/Lib/Behavior/DenyIpBehavior.class.js +++ b/lib/Lib/Behavior/DenyIpBehavior.class.js @@ -23,8 +23,8 @@ module.exports = Behavior(function(){ throw_error({ type: "deny", msg: "deny ip", - code: 403 - }) + code: 403, + }, this.http); }; return true; } diff --git a/lib/Lib/Core/App.class.js b/lib/Lib/Core/App.class.js index fb4fd397..c122ff0c 100644 --- a/lib/Lib/Core/App.class.js +++ b/lib/Lib/Core/App.class.js @@ -26,7 +26,7 @@ var App = module.exports = Class(function(){ if (!module) { module = A(group + "Empty", this.http); if (!module) { - throw_error(this.http.req.module + " module not found"); + throw_error(this.http.req.module + " module not found", this.http); }; }; var action = this.http.req.action; @@ -44,11 +44,11 @@ var App = module.exports = Class(function(){ return false; }) if (flag) { - throw_error("action black"); + throw_error("action black", this.http); }; }; if (!/^[A-Za-z](\w)*$/.test(action)) { - throw_error('action name error'); + throw_error('action name error', this.http); }; if (typeof module[action] == 'function') { if (C('url_params_bind')) { @@ -76,7 +76,7 @@ var App = module.exports = Class(function(){ return module[C('call_method')](action); }; } - throw_error("action: "+action+" not found"); + throw_error("action: "+action+" not found", this.http); }, //加载自定义外部文件 loadExtFile: function(){ diff --git a/lib/Lib/Core/Think.class.js b/lib/Lib/Core/Think.class.js index 443d3b30..84566531 100644 --- a/lib/Lib/Core/Think.class.js +++ b/lib/Lib/Core/Think.class.js @@ -11,28 +11,25 @@ module.exports = { }, register_exception: function(){ process.on('uncaughtException', function(err) { - if (err instanceof Error) { - var message = err.message; - try{ - var obj = JSON.parse(message); - switch(obj.type){ - case "deny": - __response.statusCode = obj.code || 403; - __response.write(obj.msg, C('encoding')); - break; - case "redirect": - __response.statusCode = obj.code || 302; - __response.setHeader("Location", obj.msg); - break; - case "error": - __response.write(err.stack, C('encoding')); - break; - } - }catch(e){ - console.log(err.stack); + if (typeof err && err.http) { + var http = err.http; + switch(err.type){ + case "deny": + http._res.statusCode = err.code || 403; + http._res.write(err.msg, C('encoding')); + break; + case "redirect": + http._res.statusCode = err.code || 302; + http._res.setHeader("Location", err.msg); + break; + default: + http._res.statusCode = err.code || 500; + http._res.write(err.msg, C('encoding')); } + err.http._res.end(); + return; }; - //__response.end(); + console.log(err.stack); }); }, buildApp: function(){