UNPKG

ajsfw

Version:
168 lines (167 loc) 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Exception = (function () { function Exception(messageOrParentException, parentException) { try { throw new Error("Error"); } catch (e) { this._stack = this._getStack(e); } if (this._stack !== null) { this._name = this._stack.caller; if (this._stack.child !== null) { this._stack = this._stack.child; } } this._parentException = null; if (parentException) { if (parentException instanceof Exception) { this._parentException = parentException; } if (parentException instanceof Error) { var exception = new Exception(parentException.name); exception._name = parentException.name; exception._message = parentException.message; exception._stack = this._getStack(parentException); this._parentException = exception; } } else { if (messageOrParentException instanceof Exception) { this._parentException = messageOrParentException; } if (messageOrParentException instanceof Error) { var exception = new Exception(messageOrParentException.name); exception._name = messageOrParentException.name; exception._message = messageOrParentException.message; exception._stack = this._getStack(messageOrParentException); this._parentException = exception; } } if (typeof (messageOrParentException) === "string") { this._message = messageOrParentException; } } Object.defineProperty(Exception.prototype, "parentException", { get: function () { return this._parentException; }, enumerable: true, configurable: true }); Object.defineProperty(Exception.prototype, "name", { get: function () { return this._name; }, enumerable: true, configurable: true }); Object.defineProperty(Exception.prototype, "message", { get: function () { return this._message; }, enumerable: true, configurable: true }); Object.defineProperty(Exception.prototype, "stack", { get: function () { return this._stack; }, enumerable: true, configurable: true }); Exception.throwAsync = function (exception) { setTimeout(function () { throw exception; }, 0); }; Exception.prototype._getStack = function (e) { if (e.stack === undefined) { return this._getUnknownStackInfo(); } var m; m = e.stack.match(/at /gm); if (m !== null && m.length > 0) { return this._getDefaultStackInfo(e.stack); } m = e.stack.match(/\@http/gm); if (m !== null && m.length > 0) { return this._getFirefoxStackInfo(e.stack); } return this._getUnknownStackInfo(); }; Exception.prototype._getUnknownStackInfo = function () { return { caller: "unknown", file: "unknown", line: -1, character: -1, parent: null, child: null }; }; Exception.prototype._getDefaultStackInfo = function (stack) { var stackInfo = null; var lastStackInfo = null; var calls = stack.split("\n"); for (var _i = 0, calls_1 = calls; _i < calls_1.length; _i++) { var call = calls_1[_i]; if (call.indexOf("at ") !== -1 && call.indexOf("http") !== -1) { var split1 = call.trim().replace(")", "").substr(3).split("("); if (split1.length === 1) { split1 = ["anonymous", split1[0]]; } var split2 = split1[1].split(":"); var si = { caller: split1[0].trim(), file: (split2[0] + ":" + split2[1]).trim(), line: parseInt(split2[2], 10), character: parseInt(split2[3], 10), child: null, parent: lastStackInfo }; if (lastStackInfo !== null) { lastStackInfo.child = si; } lastStackInfo = si; if (stackInfo === null) { stackInfo = lastStackInfo; } } } if (stackInfo === null) { return this._getUnknownStackInfo(); } else { return stackInfo; } }; Exception.prototype._getFirefoxStackInfo = function (stack) { var stackInfo = null; var lastStackInfo = null; var calls = stack.split("\n"); for (var _i = 0, calls_2 = calls; _i < calls_2.length; _i++) { var call = calls_2[_i]; if (call.indexOf("@") !== -1 && call.indexOf("http") !== -1) { var split1 = call.split("@"); var split2 = split1[1].split(":"); var si = { caller: split1[0].trim(), file: (split2[0] + ":" + split2[1]).trim(), line: parseInt(split2[2], 10), character: parseInt(split2[3], 10), child: null, parent: lastStackInfo }; if (lastStackInfo !== null) { lastStackInfo.child = si; } lastStackInfo = si; if (stackInfo === null) { stackInfo = lastStackInfo; } } } if (stackInfo === null) { return this._getUnknownStackInfo(); } else { return stackInfo; } }; return Exception; }()); exports.Exception = Exception;