twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

nodeback.js (1560B)


      1 "use strict";
      2 var util = require("./util");
      3 var maybeWrapAsError = util.maybeWrapAsError;
      4 var errors = require("./errors");
      5 var OperationalError = errors.OperationalError;
      6 var es5 = require("./es5");
      7 
      8 function isUntypedError(obj) {
      9     return obj instanceof Error &&
     10         es5.getPrototypeOf(obj) === Error.prototype;
     11 }
     12 
     13 var rErrorKey = /^(?:name|message|stack|cause)$/;
     14 function wrapAsOperationalError(obj) {
     15     var ret;
     16     if (isUntypedError(obj)) {
     17         ret = new OperationalError(obj);
     18         ret.name = obj.name;
     19         ret.message = obj.message;
     20         ret.stack = obj.stack;
     21         var keys = es5.keys(obj);
     22         for (var i = 0; i < keys.length; ++i) {
     23             var key = keys[i];
     24             if (!rErrorKey.test(key)) {
     25                 ret[key] = obj[key];
     26             }
     27         }
     28         return ret;
     29     }
     30     util.markAsOriginatingFromRejection(obj);
     31     return obj;
     32 }
     33 
     34 function nodebackForPromise(promise, multiArgs) {
     35     return function(err, value) {
     36         if (promise === null) return;
     37         if (err) {
     38             var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
     39             promise._attachExtraTrace(wrapped);
     40             promise._reject(wrapped);
     41         } else if (!multiArgs) {
     42             promise._fulfill(value);
     43         } else {
     44             var $_len = arguments.length;var args = new Array(Math.max($_len - 1, 0)); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];};
     45             promise._fulfill(args);
     46         }
     47         promise = null;
     48     };
     49 }
     50 
     51 module.exports = nodebackForPromise;