|
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 const errors = require("sdk/deprecated/errors"); |
|
7 |
|
8 exports.testCatchAndLog = function(assert) { |
|
9 var caught = []; |
|
10 function dummyLog(e) { caught.push(e); } |
|
11 |
|
12 var wrapped = errors.catchAndLog(function(x) { |
|
13 throw Error("blah" + x + this); |
|
14 }, "boop", dummyLog); |
|
15 |
|
16 assert.equal(wrapped.call("hi", 1), "boop", |
|
17 "exceptions should be trapped, def. resp. returned"); |
|
18 assert.equal(caught.length, 1, |
|
19 "logging function should be called"); |
|
20 assert.equal(caught[0].message, "blah1hi", |
|
21 "args and this should be passed to wrapped func"); |
|
22 }; |
|
23 |
|
24 exports.testCatchAndLogProps = function(assert) { |
|
25 var caught = []; |
|
26 function dummyLog(e) { caught.push(e); } |
|
27 |
|
28 var thing = { |
|
29 foo: function(x) { throw Error("nowai" + x); }, |
|
30 bar: function() { throw Error("blah"); }, |
|
31 baz: function() { throw Error("fnarg"); } |
|
32 }; |
|
33 |
|
34 errors.catchAndLogProps(thing, "foo", "ugh", dummyLog); |
|
35 |
|
36 assert.equal(thing.foo(1), "ugh", |
|
37 "props should be wrapped"); |
|
38 assert.equal(caught.length, 1, |
|
39 "logging function should be called"); |
|
40 assert.equal(caught[0].message, "nowai1", |
|
41 "args should be passed to wrapped func"); |
|
42 assert.throws(function() { thing.bar(); }, |
|
43 /blah/, |
|
44 "non-wrapped props should be wrapped"); |
|
45 |
|
46 errors.catchAndLogProps(thing, ["bar", "baz"], "err", dummyLog); |
|
47 assert.ok((thing.bar() == thing.baz()) && |
|
48 (thing.bar() == "err"), |
|
49 "multiple props should be wrapped if array passed in"); |
|
50 }; |
|
51 |
|
52 exports.testCatchAndReturn = function(assert) { |
|
53 var wrapped = errors.catchAndReturn(function(x) { |
|
54 if (x == 1) |
|
55 return "one"; |
|
56 if (x == 2) |
|
57 throw new Error("two"); |
|
58 return this + x; |
|
59 }); |
|
60 |
|
61 assert.equal(wrapped(1).returnValue, "one", |
|
62 "arg should be passed; return value should be returned"); |
|
63 assert.ok(wrapped(2).exception, "exception should be returned"); |
|
64 assert.equal(wrapped(2).exception.message, "two", "message is correct"); |
|
65 assert.ok(wrapped(2).exception.fileName.indexOf("test-errors.js") != -1, |
|
66 "filename is present"); |
|
67 assert.ok(wrapped(2).exception.stack, "stack is available"); |
|
68 assert.equal(wrapped.call("hi", 3).returnValue, "hi3", |
|
69 "`this` should be set correctly"); |
|
70 }; |
|
71 |
|
72 require("sdk/test").run(exports); |