Created
June 22, 2012 20:07
-
-
Save ewheeler/2974886 to your computer and use it in GitHub Desktop.
capture client-side javascript errors and log to server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // for support before ECMA-262 5th edition ... | |
| if (!Date.now) { | |
| Date.now = function now() { | |
| return +(new Date); | |
| }; | |
| } | |
| // keep track of how many errors have been logged to server | |
| var num_errors = 0; | |
| var max_errors = 10; | |
| var error_batch = 1; | |
| // keep track of when last error occured | |
| var last_error = 0; | |
| // throttle error reports occuring in quick succession | |
| var single_throttle = new Number(1000); | |
| // allow another batch of 'max_errors' if 5 seconds have elapsed | |
| // since the last error | |
| var batch_throttle = new Number(5000); | |
| try { | |
| window.onerror = function(msg, url, line){ | |
| // do not send errors that have occured in the last 'single_throttle' seconds. | |
| // use absolute value of time difference in case browser clock is wonky | |
| if (Math.abs(Date.now() - last_error) > single_throttle){ | |
| // if error limit has been reached and a new error occurs | |
| // more than 'batch_throttle' seconds later than previous error, | |
| // increment 'error_batch' to allow another batch of 'max_errors' | |
| if ((num_errors >= (max_errors*error_batch)) && (Math.abs(Date.now() - last_error) >= batch_throttle)) { | |
| error_batch = error_batch + 1; | |
| } | |
| // increment error count | |
| num_errors = num_errors + 1; | |
| // reset time of last_error | |
| last_error = Date.now(); | |
| // only report a finite number of errors so the browser | |
| // isn't overwhelmed loading so many gifs, and so we don't | |
| // ddos our server (stop after first 'max_errors' worth of errors) | |
| if (num_errors < (max_errors*error_batch)){ | |
| var sid; | |
| if (document.getElementById('session-id')){ | |
| sid = document.getElementById('session-id').innerHTML; | |
| } | |
| // prepare payload, encode everything except numbers | |
| var payload = '/jserrors?msg='+encodeURIComponent(msg)+ | |
| '&url='+encodeURIComponent(url)+'&line='+line+ | |
| '&ref='+encodeURIComponent(document.referrer)+'&ua='+encodeURIComponent(navigator.userAgent)+ | |
| '&proto='+encodeURIComponent(window.location.protocol)+'&e='+num_errors+ | |
| '&last='+last_error; | |
| // if session id was found on the page, append it to payload | |
| if (!(typeof sid === undefined)){ | |
| payload = payload + '&sid='+encodeURIComponent(sid); | |
| }; | |
| // fetch image from payload url | |
| (new Image).src = payload; | |
| }; | |
| }; | |
| // suppress browser error messages by returning true | |
| return true; | |
| }; | |
| } catch(e) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment