Code coverage report for lib/common/errors.js

Statements: 41.49% (39 / 94)      Branches: 17.14% (6 / 35)      Functions: 61.54% (8 / 13)      Lines: 41.49% (39 / 94)      Ignored: none     

All files » lib/common/ » errors.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134    1 1 1   1 1                                                                               1 1                                                 1 1 1 58   1 5 5     5 5 1   5 5 5 5 5 5 5 5   1 4 4     4 4   1 10 10 10 10                             1           1   1 1  
///<reference path=".d.ts"/>
"use strict";
var util = require("util");
var path = require("path");
function Exception() {
}
Exception.prototype = new Error();
function resolveCallStack(error) {
    var stackLines = error.stack.split("\n");
    var parsed = _.map(stackLines, function (line) {
        var match = line.match(/^\s*at ([^(]*) \((.*?):([0-9]+):([0-9]+)\)$/);
        if (match) {
            return match;
        }
        match = line.match(/^\s*at (.*?):([0-9]+):([0-9]+)$/);
        if (match) {
            match.splice(1, 0, "<anonymous>");
            return match;
        }
        return line;
    });
    var SourceMapConsumer = require("./vendor/source-map").sourceMap.SourceMapConsumer;
    var fs = require("fs");
    var remapped = _.map(parsed, function (parsedLine) {
        if (_.isString(parsedLine)) {
            return parsedLine;
        }
        var functionName = parsedLine[1];
        var fileName = parsedLine[2];
        var line = +parsedLine[3];
        var column = +parsedLine[4];
        var mapFileName = fileName + ".map";
        if (!fs.existsSync(mapFileName)) {
            return parsedLine.input;
        }
        var mapData = JSON.parse(fs.readFileSync(mapFileName).toString());
        var consumer = new SourceMapConsumer(mapData);
        var sourcePos = consumer.originalPositionFor({ line: line, column: column });
        var source = path.join(path.dirname(fileName), sourcePos.source);
        return util.format("    at %s (%s:%s:%s)", functionName, source, sourcePos.line, sourcePos.column);
    });
    var outputMessage = remapped.join("\n");
    if (outputMessage.indexOf(error.message) === -1) {
        outputMessage = outputMessage.replace(/Error/, "Error: " + error.message);
    }
    return outputMessage;
}
function installUncaughtExceptionListener(actionOnException) {
    process.on("uncaughtException", function (err) {
        var callstack = err.stack;
        if (callstack) {
            try {
                callstack = resolveCallStack(err);
            }
            catch (err) {
                console.error("Error while resolving callStack:", err);
            }
        }
        console.error(callstack || err.toString());
        if (!$injector.resolve("staticConfig").disableAnalytics) {
            try {
                var analyticsService = $injector.resolve("analyticsService");
                analyticsService.trackException(err, callstack);
            }
            catch (e) {
                console.error("Error while reporting exception: " + e);
            }
        }
        if (actionOnException) {
            actionOnException();
        }
    });
}
exports.installUncaughtExceptionListener = installUncaughtExceptionListener;
var Errors = (function () {
    function Errors() {
        this.printCallStack = false;
    }
    Errors.prototype.fail = function (optsOrFormatStr) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        var opts = optsOrFormatStr;
        if (_.isString(opts)) {
            opts = { formatStr: opts };
        }
        args.unshift(opts.formatStr);
        var exception = new Exception();
        exception.name = opts.name || "Exception";
        exception.message = util.format.apply(null, args);
        exception.stack = (new Error(exception.message)).stack;
        exception.errorCode = opts.errorCode || 127;
        exception.suppressCommandHelp = opts.suppressCommandHelp;
        throw exception;
    };
    Errors.prototype.failWithoutHelp = function (message) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        args.unshift(message);
        this.fail({ formatStr: util.format.apply(null, args), suppressCommandHelp: true });
    };
    Errors.prototype.beginCommand = function (action, printCommandHelp) {
        var _this = this;
        return (function () {
            try {
                return action().wait();
            }
            catch (ex) {
                var loggerLevel = $injector.resolve("logger").getLevel().toUpperCase();
                var printCallStack = _this.printCallStack || loggerLevel === "TRACE" || loggerLevel === "DEBUG";
                console.error(printCallStack
                    ? resolveCallStack(ex)
                    : "\x1B[31;1m" + ex.message + "\x1B[0m");
                if (!ex.suppressCommandHelp) {
                    printCommandHelp().wait();
                }
                process.exit(_.isNumber(ex.errorCode) ? ex.errorCode : 127);
            }
        }).future()();
    };
    Errors.prototype.verifyHeap = function (message) {
        if (global.gc) {
            console.log("verifyHeap: '%s'", message);
            global.gc();
        }
    };
    return Errors;
})();
exports.Errors = Errors;
$injector.register("errors", Errors);