Last active
December 15, 2015 03:39
-
-
Save duryjames/5196288 to your computer and use it in GitHub Desktop.
Better javascript debugging
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
| (function (window) { | |
| 'use strict'; | |
| if (!console) { | |
| console = {}; | |
| console.log = {}; | |
| } | |
| var windowOnError = window.onerror; | |
| window.onerror = function myErrorHandler(errorMsg, url, lineNumber) { | |
| var output_str = ''; | |
| if (url == '') { | |
| console.log("\nurl: " + url); | |
| console.log("errorMsg: " + errorMsg); | |
| console.log("lineNumber: " + lineNumber + "\n"); | |
| return false; | |
| } | |
| var oReq = new XMLHttpRequest(); | |
| function updateProgress(oEvent) { | |
| if (oEvent.lengthComputable) { | |
| var percentComplete = oEvent.loaded / oEvent.total; | |
| } else { | |
| // Unable to compute progress information since the total size is unknown | |
| } | |
| } | |
| function transferComplete(event) { | |
| console.log("============================================= Error.js: ============================================="); | |
| console.log("\nurl: " + url); | |
| console.log("errorMsg: " + errorMsg); | |
| console.log("lineNumber: " + lineNumber + "\n"); | |
| var data = event.currentTarget; | |
| if (typeof data.responseText != 'undefined' && typeof data.responseText.split === 'function') { | |
| var error_arr = data.responseText.split('\n'); | |
| for (var i = Math.max(0, lineNumber - 10); i < Math.min(error_arr.length - 1, lineNumber + 10); i++) { | |
| if (window.chrome) { | |
| console.log('%c ' + (i + 1) + ":" + error_arr[i], 'color:' + (i == (lineNumber - 1) ? '#FFFFFF; background: #FF3333' : '#FF3333;')); | |
| } else { | |
| console.log((i + 1) + ":" + error_arr[i]); | |
| } | |
| } | |
| } | |
| console.log("============================================= Error.js: ============================================="); | |
| } | |
| function transferFailed(event) { | |
| console.log("Error.js: An error occurred while transferring the file."); | |
| } | |
| function transferCanceled(event) { | |
| console.log("Error.js: The transfer has been canceled by the user."); | |
| } | |
| oReq.addEventListener("progress", updateProgress, false); | |
| oReq.addEventListener("load", transferComplete, false); | |
| oReq.addEventListener("error", transferFailed, false); | |
| oReq.addEventListener("abort", transferCanceled, false); | |
| oReq.open("GET", url, true); | |
| oReq.send(); | |
| if (windowOnError) | |
| // Call previous handler. | |
| return windowOnError(errorMsg, url, lineNumber); | |
| return false; | |
| } | |
| })(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment