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: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "deprecated" michael@0: }; michael@0: michael@0: function logToConsole(e) { michael@0: console.exception(e); michael@0: } michael@0: michael@0: var catchAndLog = exports.catchAndLog = function(callback, michael@0: defaultResponse, michael@0: logException) { michael@0: if (!logException) michael@0: logException = logToConsole; michael@0: michael@0: return function() { michael@0: try { michael@0: return callback.apply(this, arguments); michael@0: } catch (e) { michael@0: logException(e); michael@0: return defaultResponse; michael@0: } michael@0: }; michael@0: }; michael@0: michael@0: exports.catchAndLogProps = function catchAndLogProps(object, michael@0: props, michael@0: defaultResponse, michael@0: logException) { michael@0: if (typeof(props) == "string") michael@0: props = [props]; michael@0: props.forEach( michael@0: function(property) { michael@0: object[property] = catchAndLog(object[property], michael@0: defaultResponse, michael@0: logException); michael@0: }); michael@0: }; michael@0: michael@0: /** michael@0: * Catch and return an exception while calling the callback. If the callback michael@0: * doesn't throw, return the return value of the callback in a way that makes it michael@0: * possible to distinguish between a return value and an exception. michael@0: * michael@0: * This function is useful when you need to pass the result of a call across michael@0: * a process boundary (across which exceptions don't propagate). It probably michael@0: * doesn't need to be factored out into this module, since it is only used by michael@0: * a single caller, but putting it here works around bug 625560. michael@0: */ michael@0: exports.catchAndReturn = function(callback) { michael@0: return function() { michael@0: try { michael@0: return { returnValue: callback.apply(this, arguments) }; michael@0: } michael@0: catch (exception) { michael@0: return { exception: exception }; michael@0: } michael@0: }; michael@0: };