OLD | NEW |
1 function stripURL(url) { | 1 function stripURL(url) { |
2 return url ? url.match( /[^\/]+\/?$/ )[0] : url; | 2 return url ? url.match( /[^\/]+\/?$/ )[0] : url; |
3 } | 3 } |
4 | 4 |
5 function throwException(message) { | 5 function throwException(message) { |
6 throw new Error(message ? message : "An exception"); | 6 throw new Error(message ? message : "An exception"); |
7 } | 7 } |
8 | 8 |
9 var errorsSeen = 0; | 9 var errorsSeen = 0; |
10 function dumpOnErrorArgumentValuesAndReturn(returnValue, callback) { | 10 function dumpOnErrorArgumentValuesAndReturn(returnValue, callback) { |
11 window.onerror = function (message, url, line, column) { | 11 window.onerror = function (message, url, line, column, error) { |
12 debug("window.onerror: \"" + message + "\" at " + stripURL(url) + " (Lin
e: " + line + ", Column: " + column + ")"); | 12 debug("window.onerror: \"" + message + "\" at " + stripURL(url) + " (Lin
e: " + line + ", Column: " + column + ")"); |
| 13 if (error) |
| 14 debug(stripStackURLs(error.stack)); |
| 15 else |
| 16 debug("No stack trace."); |
13 | 17 |
14 if (callback) | 18 if (callback) |
15 callback(++errorsSeen); | 19 callback(++errorsSeen); |
16 if (returnValue) | 20 if (returnValue) |
17 debug("Returning 'true': the error should not be reported in the con
sole as an unhandled exception."); | 21 debug("Returning 'true': the error should not be reported in the con
sole as an unhandled exception.\n\n\n"); |
18 else | 22 else |
19 debug("Returning 'false': the error should be reported in the consol
e as an unhandled exception."); | 23 debug("Returning 'false': the error should be reported in the consol
e as an unhandled exception.\n\n\n"); |
20 return returnValue; | 24 return returnValue; |
21 }; | 25 }; |
22 } | 26 } |
23 | 27 |
24 function dumpErrorEventAndAllowDefault(callback) { | 28 function dumpErrorEventAndAllowDefault(callback) { |
25 window.addEventListener('error', function (e) { | 29 window.addEventListener('error', function (e) { |
26 dumpErrorEvent(e) | 30 dumpErrorEvent(e) |
27 debug("Not calling e.preventDefault(): the error should be reported in t
he console as an unhandled exception."); | 31 debug("Not calling e.preventDefault(): the error should be reported in t
he console as an unhandled exception.\n\n\n"); |
28 if (callback) | 32 if (callback) |
29 callback(++errorsSeen); | 33 callback(++errorsSeen); |
30 }); | 34 }); |
31 } | 35 } |
32 | 36 |
33 function dumpErrorEventAndPreventDefault(callback) { | 37 function dumpErrorEventAndPreventDefault(callback) { |
34 window.addEventListener('error', function (e) { | 38 window.addEventListener('error', function (e) { |
35 dumpErrorEvent(e); | 39 dumpErrorEvent(e); |
36 debug("Calling e.preventDefault(): the error should not be reported in t
he console as an unhandled exception."); | 40 debug("Calling e.preventDefault(): the error should not be reported in t
he console as an unhandled exception.\n\n\n"); |
37 e.preventDefault(); | 41 e.preventDefault(); |
38 if (callback) | 42 if (callback) |
39 callback(++errorsSeen); | 43 callback(++errorsSeen); |
40 }); | 44 }); |
41 } | 45 } |
42 | 46 |
43 var eventPassedToTheErrorListener = null; | 47 var eventPassedToTheErrorListener = null; |
44 var eventCurrentTarget = null; | 48 var eventCurrentTarget = null; |
45 function dumpErrorEvent(e) { | 49 function dumpErrorEvent(e) { |
46 debug("Handling '" + e.type + "' event (phase " + e.eventPhase + "): \"" + e
.message + "\" at " + stripURL(e.filename) + ":" + e.lineno); | 50 debug("Handling '" + e.type + "' event (phase " + e.eventPhase + "): \"" + e
.message + "\" at " + stripURL(e.filename) + ":" + e.lineno); |
| 51 if (e.error) |
| 52 debug(stripStackURLs(e.error.stack)); |
| 53 else |
| 54 debug("No stack trace."); |
47 | 55 |
48 eventPassedToTheErrorListener = e; | 56 eventPassedToTheErrorListener = e; |
49 eventCurrentTarget = e.currentTarget; | 57 eventCurrentTarget = e.currentTarget; |
50 shouldBe('eventPassedToTheErrorListener', 'window.event'); | 58 shouldBe('eventPassedToTheErrorListener', 'window.event'); |
51 shouldBe('eventCurrentTarget', 'window'); | 59 shouldBe('eventCurrentTarget', 'window'); |
52 eventPassedToTheErrorListener = null; | 60 eventPassedToTheErrorListener = null; |
53 eventCurrentTarget = null; | 61 eventCurrentTarget = null; |
54 } | 62 } |
| 63 |
| 64 function stripStackURLs(stackTrace) { |
| 65 stackTrace = stackTrace.split("\n"); |
| 66 var length = Math.min(stackTrace.length, 100); |
| 67 var text = "Stack Trace:\n"; |
| 68 for (var i = 0; i < length; i++) { |
| 69 text += stackTrace[i].replace(/at ((?:eval at \()?[a-zA-Z\.]+ )?\(?.+\/(
[^\/]+):(\d+):(\d+)\)?/, "at $1$2:$3:$4") + "\n"; |
| 70 } |
| 71 return text; |
| 72 } |
OLD | NEW |