1 | "use strict";
|
2 | var base_wrapped_exception_1 = require('./base_wrapped_exception');
|
3 | var collection_1 = require('./collection');
|
4 | var lang_1 = require('./lang');
|
5 | var _ArrayLogger = (function () {
|
6 | function _ArrayLogger() {
|
7 | this.res = [];
|
8 | }
|
9 | _ArrayLogger.prototype.log = function (s) { this.res.push(s); };
|
10 | _ArrayLogger.prototype.logError = function (s) { this.res.push(s); };
|
11 | _ArrayLogger.prototype.logGroup = function (s) { this.res.push(s); };
|
12 | _ArrayLogger.prototype.logGroupEnd = function () { };
|
13 | ;
|
14 | return _ArrayLogger;
|
15 | }());
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var ExceptionHandler = (function () {
|
39 | function ExceptionHandler(_logger, _rethrowException) {
|
40 | if (_rethrowException === void 0) { _rethrowException = true; }
|
41 | this._logger = _logger;
|
42 | this._rethrowException = _rethrowException;
|
43 | }
|
44 | ExceptionHandler.exceptionToString = function (exception, stackTrace, reason) {
|
45 | if (stackTrace === void 0) { stackTrace = null; }
|
46 | if (reason === void 0) { reason = null; }
|
47 | var l = new _ArrayLogger();
|
48 | var e = new ExceptionHandler(l, false);
|
49 | e.call(exception, stackTrace, reason);
|
50 | return l.res.join('\n');
|
51 | };
|
52 | ExceptionHandler.prototype.call = function (exception, stackTrace, reason) {
|
53 | if (stackTrace === void 0) { stackTrace = null; }
|
54 | if (reason === void 0) { reason = null; }
|
55 | var originalException = this._findOriginalException(exception);
|
56 | var originalStack = this._findOriginalStack(exception);
|
57 | var context = this._findContext(exception);
|
58 | this._logger.logGroup("EXCEPTION: " + this._extractMessage(exception));
|
59 | if (lang_1.isPresent(stackTrace) && lang_1.isBlank(originalStack)) {
|
60 | this._logger.logError('STACKTRACE:');
|
61 | this._logger.logError(this._longStackTrace(stackTrace));
|
62 | }
|
63 | if (lang_1.isPresent(reason)) {
|
64 | this._logger.logError("REASON: " + reason);
|
65 | }
|
66 | if (lang_1.isPresent(originalException)) {
|
67 | this._logger.logError("ORIGINAL EXCEPTION: " + this._extractMessage(originalException));
|
68 | }
|
69 | if (lang_1.isPresent(originalStack)) {
|
70 | this._logger.logError('ORIGINAL STACKTRACE:');
|
71 | this._logger.logError(this._longStackTrace(originalStack));
|
72 | }
|
73 | if (lang_1.isPresent(context)) {
|
74 | this._logger.logError('ERROR CONTEXT:');
|
75 | this._logger.logError(context);
|
76 | }
|
77 | this._logger.logGroupEnd();
|
78 |
|
79 |
|
80 | if (this._rethrowException)
|
81 | throw exception;
|
82 | };
|
83 |
|
84 | ExceptionHandler.prototype._extractMessage = function (exception) {
|
85 | return exception instanceof base_wrapped_exception_1.BaseWrappedException ? exception.wrapperMessage :
|
86 | exception.toString();
|
87 | };
|
88 |
|
89 | ExceptionHandler.prototype._longStackTrace = function (stackTrace) {
|
90 | return collection_1.isListLikeIterable(stackTrace) ? stackTrace.join('\n\n-----async gap-----\n') :
|
91 | stackTrace.toString();
|
92 | };
|
93 |
|
94 | ExceptionHandler.prototype._findContext = function (exception) {
|
95 | try {
|
96 | if (!(exception instanceof base_wrapped_exception_1.BaseWrappedException))
|
97 | return null;
|
98 | return lang_1.isPresent(exception.context) ? exception.context :
|
99 | this._findContext(exception.originalException);
|
100 | }
|
101 | catch (e) {
|
102 |
|
103 | return null;
|
104 | }
|
105 | };
|
106 |
|
107 | ExceptionHandler.prototype._findOriginalException = function (exception) {
|
108 | if (!(exception instanceof base_wrapped_exception_1.BaseWrappedException))
|
109 | return null;
|
110 | var e = exception.originalException;
|
111 | while (e instanceof base_wrapped_exception_1.BaseWrappedException && lang_1.isPresent(e.originalException)) {
|
112 | e = e.originalException;
|
113 | }
|
114 | return e;
|
115 | };
|
116 |
|
117 | ExceptionHandler.prototype._findOriginalStack = function (exception) {
|
118 | if (!(exception instanceof base_wrapped_exception_1.BaseWrappedException))
|
119 | return null;
|
120 | var e = exception;
|
121 | var stack = exception.originalStack;
|
122 | while (e instanceof base_wrapped_exception_1.BaseWrappedException && lang_1.isPresent(e.originalException)) {
|
123 | e = e.originalException;
|
124 | if (e instanceof base_wrapped_exception_1.BaseWrappedException && lang_1.isPresent(e.originalException)) {
|
125 | stack = e.originalStack;
|
126 | }
|
127 | }
|
128 | return stack;
|
129 | };
|
130 | return ExceptionHandler;
|
131 | }());
|
132 | exports.ExceptionHandler = ExceptionHandler;
|
133 |
|
\ | No newline at end of file |