JS handles all exceptions to the foreground page

Source: Internet
Author: User

<reference path= ". /.. /scripts/jquery-1.4.1-vsdoc.js "/>

<reference path= "Yzg.prototype.js"/>

/*

Author: Genzo

Date: 2012-4-5

Email: [Email protected]

Note: This JS is written for Genzo, can be used in any scene, can also change the code, but cannot change or remove the author's name and comments, otherwise will be held liable.

*/

(function ($) {

/*************************** Start core framework processing function ***************************************/

function Throw (name, message, info) {

if (typeof message = = "undefined") {message = name; name = "";}

Name = Name | | "Exception information";

Message = Message | | "An unknown error has occurred, please contact the developer";

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;

}

Splitting a path into a namespace array

var paths = (typeof Path = = "string")? Path.split ("."): [path];

Set the default namespace to window

var nameSpace = base;

To set a namespace path that has already been processed

var handlepaths = [];

Loop through each node in the path

while (Paths.length > 0) {

Extract a single node name for this namespace

var name = Paths.shift ();

Verify that the object is already present, that it is processed with historical objects (extending the source object when the path ends), that the new object is not present (equals the incoming object when the path ends)

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] = {};

}

}

Writes a path to a path that has already been processed

Handlepaths.push (name);

Throws an exception when the data type is incorrect

if (typeof nameSpace! = "Object") {

Throw ("Path Error", "Discovery Path [@path] already exists, and the data type is not object and cannot be namespace extended!". Replace ("@path", Handlepaths.join (".")));

}

}

return nameSpace;

}

/*************************** End Core framework processing function ***************************************/

/*************************** Start exception Handling Framework ***************************************/

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 = "Exception information";

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 Exception Handling Framework ***************************************/

var core = {

Throw:throw,

Namespace:namespace

};

NameSpace ("Yzg.core", core);

NameSpace ("Yzg.error", error);

}) (JQuery);

/*

Invoke Example

var error = Yzg.error;

Error. AddHandle (function (name, MSG, info, URL, line) {

$ (document.body). Append ("<div>" + name + msg + info.id + "</div>");

});

Error. Throw ("Advanced Exception", "Exception information", {ID: "5"});

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.