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