|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 var traceback = require("sdk/console/traceback"); |
|
7 var {Cc,Ci,Cr,Cu} = require("chrome"); |
|
8 const { on, off } = require("sdk/system/events"); |
|
9 |
|
10 function throwNsIException() { |
|
11 var ios = Cc['@mozilla.org/network/io-service;1'] |
|
12 .getService(Ci.nsIIOService); |
|
13 ios.newURI("i'm a malformed URI", null, null); |
|
14 } |
|
15 |
|
16 function throwError() { |
|
17 throw new Error("foob"); |
|
18 } |
|
19 |
|
20 exports.testFormatDoesNotFetchRemoteFiles = function(assert) { |
|
21 ["http", "https"].forEach( |
|
22 function(scheme) { |
|
23 var httpRequests = 0; |
|
24 function onHttp() { |
|
25 httpRequests++; |
|
26 } |
|
27 |
|
28 on("http-on-modify-request", onHttp); |
|
29 |
|
30 try { |
|
31 var tb = [{filename: scheme + "://www.mozilla.org/", |
|
32 lineNumber: 1, |
|
33 name: "blah"}]; |
|
34 traceback.format(tb); |
|
35 } catch (e) { |
|
36 assert.fail(e); |
|
37 } |
|
38 |
|
39 off("http-on-modify-request", onHttp); |
|
40 |
|
41 assert.equal(httpRequests, 0, |
|
42 "traceback.format() does not make " + |
|
43 scheme + " request"); |
|
44 }); |
|
45 }; |
|
46 |
|
47 exports.testFromExceptionWithString = function(assert) { |
|
48 try { |
|
49 throw "foob"; |
|
50 assert.fail("an exception should've been thrown"); |
|
51 } catch (e) { |
|
52 if (e == "foob") { |
|
53 var tb = traceback.fromException(e); |
|
54 assert.equal(tb.length, 0); |
|
55 } |
|
56 else { |
|
57 throw e; |
|
58 } |
|
59 } |
|
60 }; |
|
61 |
|
62 exports.testFormatWithString = function(assert) { |
|
63 // This can happen if e.g. a thrown exception was |
|
64 // a string instead of an Error instance. |
|
65 assert.equal(traceback.format("blah"), |
|
66 "Traceback (most recent call last):"); |
|
67 }; |
|
68 |
|
69 exports.testFromExceptionWithError = function(assert) { |
|
70 try { |
|
71 throwError(); |
|
72 assert.fail("an exception should've been thrown"); |
|
73 } catch (e) { |
|
74 if (e instanceof Error) { |
|
75 var tb = traceback.fromException(e); |
|
76 |
|
77 var xulApp = require("sdk/system/xul-app"); |
|
78 assert.equal(tb.slice(-1)[0].name, "throwError"); |
|
79 } |
|
80 else { |
|
81 throw e; |
|
82 } |
|
83 } |
|
84 }; |
|
85 |
|
86 exports.testFromExceptionWithNsIException = function(assert) { |
|
87 try { |
|
88 throwNsIException(); |
|
89 assert.fail("an exception should've been thrown"); |
|
90 } catch (e) { |
|
91 if (e.result == Cr.NS_ERROR_MALFORMED_URI) { |
|
92 var tb = traceback.fromException(e); |
|
93 assert.equal(tb[tb.length - 1].name, "throwNsIException"); |
|
94 } |
|
95 else { |
|
96 throw e; |
|
97 } |
|
98 } |
|
99 }; |
|
100 |
|
101 exports.testFormat = function(assert) { |
|
102 function getTraceback() { |
|
103 return traceback.format(); |
|
104 } |
|
105 |
|
106 var formatted = getTraceback(); |
|
107 assert.equal(typeof(formatted), "string"); |
|
108 var lines = formatted.split("\n"); |
|
109 |
|
110 assert.equal(lines[lines.length - 2].indexOf("getTraceback") > 0, |
|
111 true, |
|
112 "formatted traceback should include function name"); |
|
113 |
|
114 assert.equal(lines[lines.length - 1].trim(), |
|
115 "return traceback.format();", |
|
116 "formatted traceback should include source code"); |
|
117 }; |
|
118 |
|
119 exports.testExceptionsWithEmptyStacksAreLogged = function(assert) { |
|
120 // Ensures that our fix to bug 550368 works. |
|
121 var sandbox = Cu.Sandbox("http://www.foo.com"); |
|
122 var excRaised = false; |
|
123 try { |
|
124 Cu.evalInSandbox("returns 1 + 2;", sandbox, "1.8", |
|
125 "blah.js", 25); |
|
126 } catch (e) { |
|
127 excRaised = true; |
|
128 var stack = traceback.fromException(e); |
|
129 assert.equal(stack.length, 1, "stack should have one frame"); |
|
130 |
|
131 assert.ok(stack[0].fileName, "blah.js", "frame should have filename"); |
|
132 assert.ok(stack[0].lineNumber, 25, "frame should have line no"); |
|
133 assert.equal(stack[0].name, null, "frame should have null function name"); |
|
134 } |
|
135 if (!excRaised) |
|
136 assert.fail("Exception should have been raised."); |
|
137 }; |
|
138 |
|
139 require('sdk/test').run(exports); |