JS Bug Automatic Warning

Source: Internet
Author: User

These two days when looking at the Getcurrentscript function in AMD, suddenly thought, since can detect the current error of the JS file, then can do an automatic JS bug warning function. As long as there is a JS error appears, it will automatically upload the error to the server.

First look at Masaki Big Book wrote a section on the detection of JS file address function, related blog address point this:

functionGetcurrentscript (base) {//Reference Https://github.com/samyk/jiagra/blob/master/jiagra.js        varStack; Try{A.B.C ();//Force an error in order to capture E.stack}Catch(e) {//Safari's Error object is only Line,sourceid,sourceurlstack =E.stack; if(!stack &&Window.opera) {//Opera 9 has no e.stack, but has E. BackTrace, but cannot be obtained directly, need to extract the E object to the stringStack = (String (e). Match (/of linked script \s+/g) | | []). Join (""); }        }        if(Stack) {/**e.stack the last line in all supported browsers is roughly as follows: *chrome23: * at Http://113.93.50.63/data.js:4:1 *FIREFO             X17: * @http://113.93.50.63/query.js:4 *opera12:http://www.oldapps.com/opera.php?system=windows_xp * @http://113.93.50.63/data.js:4 *ie10: * at Global code (http://113.93.50.63/data.js: 4:1) *//firefox4+ can be used Document.currentscript*/Stack= Stack.split (/[@]/g). Pop ();//gets the last line, the last space, or the part after @stack = stack[0] = = = "("? Stack.slice (1,-1): Stack.replace (/\s/, "");//Remove line break            returnStack.replace (/(: \d+)?: \ d+$/i, "");//Remove the line number and the starting position of a typo character that may exist        }        varnodes = (base document:head). getElementsByTagName ("script");//only look in the head tag         for(vari = nodes.length, node; node = nodes[--i]; ) {            if(Base | | node.classname = = = Moduleclass) && Node.readystate = = = "Interactive") {                returnNode.classname =node.src; }        }    }

Behind the node manipulation is to be compatible with IE, we do not care about the pit dad's ie, first to see let us in a pleasant mood of modern browser

In the above function, it can be found that e.stack is simply a big kill, basically as long as the analysis inside the content, you can get specific error location

But this function execution is conditional, we must let it be loaded in the JS file environment to execute, so that the file will be reported errors

In order to simulate this environment, I have a handy copy of a amd,cmd in the use of the function define~

functiondefine (callback) {Try{callback (); }Catch(e) {varStack; Stack=E.stack; if(!stack &&Window.opera) {Stack= (String (e). Match (/of linked script \s+/g) | | []). Join (""); }        if(Stack) {stack= Stack.split (/[@]/g). Pop (); Stack= Stack[0] = = = ' ('? Stack.slice (1,-1): Stack.replace (/\s/, ""); Stack= Stack.replace (/(: \d+)?: \ d*$/i, ");            Bug_upload (stack); return; }    }}

The inside of the bug_upload is used to deal with the wrong path to get, here for convenience, only the path alert out.

Then we put the load as far as possible JS with define package, and then in the test code to add a random error:

Define (function() {    function  test_bug () {        a.b.c ();    }    Test_bug ();});

Exact Popup Error Address:

Wow, that's great! Then we're going to take care of IE.

The new version of IE has already supported e.stack, we first do the old version of IE!

Consider the IE error when the old pop-up box, said your page has errors, it is very annoying! So I felt tied to him a onerror event:

function (msg,url) {    bug_upload (URL);     return true ;}

How to put this thing in the original detection of the large function, hey:

functiondefine (callback) {Try{callback ()}Catch(e) {varStack; Stack=E.stack; if(!stack &&Window.opera) {Stack= (String (e). Match (/of linked script \s+/g) | | []). Join (""); }        if(Stack) {stack= Stack.split (/[@]/g). Pop (); Stack= Stack[0] = = = ' ('? Stack.slice (1,-1): Stack.replace (/\s/, ""); Stack= Stack.replace (/(: \d+)?: \ d*$/i, ");            Bug_upload (stack); return; } window.onerror=function(Msg,url) {bug_upload (URL); return true; } window.fireevent&& window.fireevent (' onerror ', E); }}functionbug_upload (path) {alert (path);}

Get!! Although the old version of IE here will only pop up the current error HTML .... The problem is always solved in one step.

Hide it ~

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.