Server-side crash on client side Javascript Crashes

I have a large complex web application with thousands of Javascript lines. There is a small set of intermittent Javascript errors that inform users.

I think this is an epiphenomenon of the race conditions - something was not correctly initialized, and Javascript errors causing "down stream" js did not start.

Anyway, to execute Javascript crash for server side registration?

All js log libraries such as Blackbird and Log4JavaScript are client only.

+4
source share
2 answers

I wrote a remote error logging function using window.onerror as suggested by @pimvdb

Err = {}; Err.Remoterr = {}; Err.Remoterr.onerror = function (msg, errorfileurl, lineno) { var jsonstring, response, pageurl, cookies; // Get some user input response = prompt("There has been an error. " + "It has been logged and will be investigated.", "Put in comments (and e-mail or phone number for" + " response.)"); // get some context of where and how the error occured // to make debugging easier pageurl = window.location.href; cookies = document.cookie; // Make the json message we are going to post // Could use JSON.stringify() here if you are sure that // JSON will have run when the error occurs // http://www.JSON.org/js.html jsonstring = "{\"set\": {\"jserr\": " + "{\"msg\": \"" + msg + "\", " + "\"errorfileurl\": \"" + errorfileurl + "\", " + "\"pageurl\": \"" + pageurl + "\", " + "\"cookies\": \"" + cookies + "\", " + "\"lineno\": \"" + lineno + "\", " + "\"response\": \"" + response + "\"}}}"; // Use the jquery cross-browser post // http://api.jquery.com/jQuery.post/ // this assumes that no errors happen before jquery has initialised $.post("?jserr", jsonstring, null, "json"); // I don't want the page to 'pretend' to work // so I am going to return 'false' here // Returning 'true' will clear the error in the browser return false; }; window.onerror = Err.Remoterr.onerror; 

I use this between the head and body tags of a web page.

You want to change the JSON and URL that you publish, depending on how you are going to register the data server.

+3
source

Take a look at https://log4sure.com (disclosure: I created it) - but it is really useful, check and decide for yourself. It allows you to log errors / events, and also allows you to create your own log table. It also allows you to track your logs in real time. And the best part is it's free.

You can also use bower to install it, use bower install log4sure

The generated code is also very simple:

 // setup var _logServer; (function() { var ls = document.createElement('script'); ls.type = 'text/javascript'; ls.async = true; ls.src = 'https://log4sure.com/ScriptsExt/log4sure.min.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ls, s); ls.onload = function() { // use your token here. _logServer = new LogServer("use-your-token-here"); }; })(); // example for logging text _logServer.logText("your log message goes here.") //example for logging error divide = function(numerator, divisor) { try { if (parseFloat(value) && parseFloat(divisor)) { throw new TypeError("Invalid input", "myfile.js", 12, { value: value, divisor: divisor }); } else { if (divisor == 0) { throw new RangeError("Divide by 0", "myfile.js", 15, { value: value, divisor: divisor }); } } } catch (e) { _logServer.logError(e.name, e.message, e.stack); } } // another use of logError in window.onerror // must be careful with window.onerror as you might be overwriting some one else window.onerror functionality // also someone else can overwrite window.onerror. window.onerror = function(msg, url, line, column, err) { // may want to check if url belongs to your javascript file var data = { url: url, line: line, column: column, } _logServer.logError(err.name, err.message, err.stack, data); }; // example for custom logs var foo = "some variable value"; var bar = "another variable value"; var flag = "false"; var temp = "yet another variable value"; _logServer.log(foo, bar, flag, temp); 
+2
source

Source: https://habr.com/ru/post/1387351/


All Articles