/// <reference path="../../Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="yzg.prototype.js" />
/*
作者 : 颜佐光
时间 : 2012-4-5
Email : [email protected]
备注 : 本JS为颜佐光所编写,可以用于任何场景,也可以更改代码,但是不能更改或者去掉作者的名字和备注,否则将追究法律责任。
*/
(function ($) {
/*************************** start 核心框架处理函数 ***************************************/
function Throw(name, message, info) {
if (typeof message == "undefined") { message = name; name = ""; }
name = name || "异常信息";
message = message || "发生未知错误,请联系开发人员";
var err = new Error();
err.name = name;
err.message = message;
err.info = info;
throw err;
}
function NameSpace(base, path, obj) {
if (typeof base == "string" || typeof base == "number") {
obj = path; path = base, base = undefined;
}
if (typeof base == "undefined") {
base = window;
}
//将路径拆分为命名空间数组
var paths = (typeof path == "string") ? path.split(".") : [path];
//设置默认的命名空间为window
var nameSpace = base;
//设置已经处理过的命名空间路径
var handlePaths = [];
//循环处理路径中的每个节点
while (paths.length > 0) {
//提取该命名空间的单个节点名称
var name = paths.shift();
//验证是否已经存在对象,存在则拿历史对象处理(当路径结束时对源对象进行扩展),不存在则新建立对象(当路径结束时等于传入的对象)
if (typeof nameSpace[name] != "undefined") {
nameSpace = nameSpace[name];
if (obj && paths.length == 0 && typeof nameSpace == "object") {
$.extend(nameSpace, obj);
}
}
else {
if (obj && paths.length == 0) {
nameSpace = nameSpace[name] = obj;
}
else {
nameSpace = nameSpace[name] = {};
}
}
//将路径写入已经处理的路径
handlePaths.push(name);
//将数据类型不正确时抛出异常
if (typeof nameSpace != "object") {
Throw("路径错误", "发现路径[@path]已经存在数据,并且该数据的数据类型不为Object,不能进行命名空间扩展!".replace("@path", handlePaths.join(".")));
}
}
return nameSpace;
}
/*************************** end 核心框架处理函数 ***************************************/
/*************************** start 异常处理框架 ***************************************/
function ErrorQueue() {
this.errorQueue = [];
this.errorHandle = [];
this.Throw = function (name, message, info) {
try {
Throw(name, message, info);
} catch (e) {
var len = this.errorQueue.length;
this.errorQueue.push(e);
Throw("" + len);
}
};
this.Get = function (obj) {
var queueName = obj && (obj.message || obj);
var index = parseInt(queueName);
return this.errorQueue[index];
};
this.AddHandle = function (func) {
if (typeof func == "function") {
this.errorHandle.push(func);
}
};
this.DefaultHandle = function (name, msg, info, url, line) {
alert("Url : " + url + "\nLine : " + line + "\nName:\n" + name + "\nMsg:\n" + msg);
};
this.Handle = function (msg, url, line) {
var e = this.Get(msg);
var name = "异常信息";
var info = null;
if (e) {
msg = e.message;
name = e.name;
info = e.info;
}
try {
if (this.errorHandle.length == 0) {
throw new Error();
}
for (var i = 0; i < this.errorHandle.length; i++) {
var func = this.errorHandle[i];
if (typeof func == "function") {
func(name, msg, info, url, line);
}
}
} catch (ex) {
this.DefaultHandle(name, msg, info, url, line);
}
};
}
var error = new ErrorQueue();
window.onerror = function (msg, url, line) {
error.Handle.call(error, msg, url, line);
};
/*************************** end 异常处理框架 ***************************************/
var core = {
Throw: Throw,
NameSpace: NameSpace
};
NameSpace("yzg.core", core);
NameSpace("yzg.error", error);
})(jQuery);
/*
//调用示例
var error = yzg.error;
error.AddHandle(function (name, msg, info, url, line) {
$(document.body).append("<div>" + name + msg + info.id + "</div>");
});
error.Throw("高级异常", "异常信息", { id: "5" });
*/
JS处理前台页面的所有异常