michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const errors = require("sdk/deprecated/errors"); michael@0: michael@0: exports.testCatchAndLog = function(assert) { michael@0: var caught = []; michael@0: function dummyLog(e) { caught.push(e); } michael@0: michael@0: var wrapped = errors.catchAndLog(function(x) { michael@0: throw Error("blah" + x + this); michael@0: }, "boop", dummyLog); michael@0: michael@0: assert.equal(wrapped.call("hi", 1), "boop", michael@0: "exceptions should be trapped, def. resp. returned"); michael@0: assert.equal(caught.length, 1, michael@0: "logging function should be called"); michael@0: assert.equal(caught[0].message, "blah1hi", michael@0: "args and this should be passed to wrapped func"); michael@0: }; michael@0: michael@0: exports.testCatchAndLogProps = function(assert) { michael@0: var caught = []; michael@0: function dummyLog(e) { caught.push(e); } michael@0: michael@0: var thing = { michael@0: foo: function(x) { throw Error("nowai" + x); }, michael@0: bar: function() { throw Error("blah"); }, michael@0: baz: function() { throw Error("fnarg"); } michael@0: }; michael@0: michael@0: errors.catchAndLogProps(thing, "foo", "ugh", dummyLog); michael@0: michael@0: assert.equal(thing.foo(1), "ugh", michael@0: "props should be wrapped"); michael@0: assert.equal(caught.length, 1, michael@0: "logging function should be called"); michael@0: assert.equal(caught[0].message, "nowai1", michael@0: "args should be passed to wrapped func"); michael@0: assert.throws(function() { thing.bar(); }, michael@0: /blah/, michael@0: "non-wrapped props should be wrapped"); michael@0: michael@0: errors.catchAndLogProps(thing, ["bar", "baz"], "err", dummyLog); michael@0: assert.ok((thing.bar() == thing.baz()) && michael@0: (thing.bar() == "err"), michael@0: "multiple props should be wrapped if array passed in"); michael@0: }; michael@0: michael@0: exports.testCatchAndReturn = function(assert) { michael@0: var wrapped = errors.catchAndReturn(function(x) { michael@0: if (x == 1) michael@0: return "one"; michael@0: if (x == 2) michael@0: throw new Error("two"); michael@0: return this + x; michael@0: }); michael@0: michael@0: assert.equal(wrapped(1).returnValue, "one", michael@0: "arg should be passed; return value should be returned"); michael@0: assert.ok(wrapped(2).exception, "exception should be returned"); michael@0: assert.equal(wrapped(2).exception.message, "two", "message is correct"); michael@0: assert.ok(wrapped(2).exception.fileName.indexOf("test-errors.js") != -1, michael@0: "filename is present"); michael@0: assert.ok(wrapped(2).exception.stack, "stack is available"); michael@0: assert.equal(wrapped.call("hi", 3).returnValue, "hi3", michael@0: "`this` should be set correctly"); michael@0: }; michael@0: michael@0: require("sdk/test").run(exports);