UNPKG

5.56 kBJavaScriptView Raw
1"use strict";
2var base_wrapped_exception_1 = require('./base_wrapped_exception');
3var collection_1 = require('./collection');
4var lang_1 = require('./lang');
5var _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 * Provides a hook for centralized exception handling.
18 *
19 * The default implementation of `ExceptionHandler` prints error messages to the `Console`. To
20 * intercept error handling,
21 * write a custom exception handler that replaces this default as appropriate for your app.
22 *
23 * ### Example
24 *
25 * ```javascript
26 *
27 * class MyExceptionHandler implements ExceptionHandler {
28 * call(error, stackTrace = null, reason = null) {
29 * // do something with the exception
30 * }
31 * }
32 *
33 * bootstrap(MyApp, {provide: ExceptionHandler, useClass: MyExceptionHandler}])
34 *
35 * ```
36 * @stable
37 */
38var 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 // We rethrow exceptions, so operations like 'bootstrap' will result in an error
79 // when an exception happens. If we do not rethrow, bootstrap will always succeed.
80 if (this._rethrowException)
81 throw exception;
82 };
83 /** @internal */
84 ExceptionHandler.prototype._extractMessage = function (exception) {
85 return exception instanceof base_wrapped_exception_1.BaseWrappedException ? exception.wrapperMessage :
86 exception.toString();
87 };
88 /** @internal */
89 ExceptionHandler.prototype._longStackTrace = function (stackTrace) {
90 return collection_1.isListLikeIterable(stackTrace) ? stackTrace.join('\n\n-----async gap-----\n') :
91 stackTrace.toString();
92 };
93 /** @internal */
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 // exception.context can throw an exception. if it happens, we ignore the context.
103 return null;
104 }
105 };
106 /** @internal */
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 /** @internal */
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}());
132exports.ExceptionHandler = ExceptionHandler;
133//# sourceMappingURL=exception_handler.js.map
\No newline at end of file