|
| 1 | +<?xml version="1.0"?> |
| 2 | +<!DOCTYPE html> |
| 3 | +<html> |
| 4 | + <head> |
| 5 | + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> |
| 6 | + <title>stacktrace.js functional tests</title> |
| 7 | + <style>li{font-size:14px;font-weight:bold}</style> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + <h3><a href="https://github.com/eriwen">eriwen</a> / <strong><a href="https://github.com/eriwen/javascript-stacktrace">stacktrace.js</a></strong></h3> |
| 11 | + <ul> |
| 12 | + <li>Just include stacktrace.js file on your page, and call it like so:</li> |
| 13 | + </ul> |
| 14 | + <code> |
| 15 | + <pre><script type="text/javascript" src="path/to/stacktrace.js" /> |
| 16 | +<script type="text/javascript"> |
| 17 | +... your code ... |
| 18 | +if (errorCondition) { |
| 19 | + var trace = printStackTrace(); |
| 20 | + //Output however you want! |
| 21 | + alert(trace.join('\n\n')); |
| 22 | +} |
| 23 | +... more code of yours ... |
| 24 | +</script></pre> |
| 25 | + </code> |
| 26 | + <p>Tested in <a href="testcase1.html" target="_blank">Plain test</a></p> |
| 27 | + <ul> |
| 28 | + <li>You can also pass in your own Error to get a stacktrace:</li> |
| 29 | + </ul> |
| 30 | + <code> |
| 31 | + <pre><script type="text/javascript"> |
| 32 | + var lastError; |
| 33 | + try { |
| 34 | + // error producing code |
| 35 | + } catch(e) { |
| 36 | + lastError = e; |
| 37 | + // do something else with error |
| 38 | + } |
| 39 | + // Returns stacktrace from lastError! |
| 40 | + printStackTrace({e: lastError}); |
| 41 | +</script></pre> |
| 42 | + </code> |
| 43 | + <p>Tested in <a href="testcase2.html" target="_blank">passing error test</a></p> |
| 44 | + <ul> |
| 45 | + <li>Some people recommend just assigning it to window.onerror (Only in IE and FF):</li> |
| 46 | + </ul> |
| 47 | + <code> |
| 48 | + <pre>window.onerror = function(msg, file, line) { |
| 49 | + alert(printStackTrace().join('\n\n')); |
| 50 | + }</pre> |
| 51 | + </code> |
| 52 | + <p>Tested in <a href="testcase3.html" target="_blank">window.onerror test</a></p> |
| 53 | + <ul> |
| 54 | + <li>You can now have any (public or privileged) function give you a stacktrace when it is called:</li> |
| 55 | + </ul> |
| 56 | + <code> |
| 57 | + <pre>var p = new printStackTrace.implementation(); |
| 58 | + p.instrumentFunction(this, 'bar', logStackTrace); |
| 59 | +function logStackTrace(stack) { |
| 60 | + console.log(stack.join('\n')); |
| 61 | + } function foo() { |
| 62 | + var a = 1; |
| 63 | + bar(); |
| 64 | + } |
| 65 | +function bar() { |
| 66 | + baz(); |
| 67 | + } |
| 68 | +foo(); //Will log a stacktrace when 'bar()' is called containing 'foo()'! |
| 69 | + p.deinstrumentFunction(this, 'bar'); //Remove function instrumentation</pre> |
| 70 | + </code> |
| 71 | + <p>Tested in <a href="testcase4.html" target="_blank">function instrumentation test</a></p> |
| 72 | + </body> |
| 73 | +</html> |
0 commit comments