addon-sdk/source/lib/sdk/deprecated/errors.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "deprecated"
     9 };
    11 function logToConsole(e) {
    12   console.exception(e);
    13 }
    15 var catchAndLog = exports.catchAndLog = function(callback,
    16                                                  defaultResponse,
    17                                                  logException) {
    18   if (!logException)
    19     logException = logToConsole;
    21   return function() {
    22     try {
    23       return callback.apply(this, arguments);
    24     } catch (e) {
    25       logException(e);
    26       return defaultResponse;
    27     }
    28   };
    29 };
    31 exports.catchAndLogProps = function catchAndLogProps(object,
    32                                                      props,
    33                                                      defaultResponse,
    34                                                      logException) {
    35   if (typeof(props) == "string")
    36     props = [props];
    37   props.forEach(
    38     function(property) {
    39       object[property] = catchAndLog(object[property],
    40                                      defaultResponse,
    41                                      logException);
    42     });
    43 };
    45 /**
    46  * Catch and return an exception while calling the callback.  If the callback
    47  * doesn't throw, return the return value of the callback in a way that makes it
    48  * possible to distinguish between a return value and an exception.
    49  *
    50  * This function is useful when you need to pass the result of a call across
    51  * a process boundary (across which exceptions don't propagate).  It probably
    52  * doesn't need to be factored out into this module, since it is only used by
    53  * a single caller, but putting it here works around bug 625560.
    54  */
    55 exports.catchAndReturn = function(callback) {
    56   return function() {
    57     try {
    58       return { returnValue: callback.apply(this, arguments) };
    59     }
    60     catch (exception) {
    61       return { exception: exception };
    62     }
    63   };
    64 };

mercurial