UNPKG

3.86 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8/**
9 * \@whatItDoes Provides a hook for centralized exception handling.
10 *
11 * \@description
12 *
13 * The default implementation of `ErrorHandler` prints error messages to the `console`. To
14 * intercept error handling, write a custom exception handler that replaces this default as
15 * appropriate for your app.
16 *
17 * ### Example
18 *
19 * ```
20 * class MyErrorHandler implements ErrorHandler {
21 * handleError(error) {
22 * // do something with the exception
23 * }
24 * }
25 *
26 * \@NgModule({
27 * providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
28 * })
29 * class MyModule {}
30 * ```
31 *
32 * \@stable
33 */
34export var ErrorHandler = (function () {
35 /**
36 * @param {?=} rethrowError
37 */
38 function ErrorHandler(rethrowError) {
39 if (rethrowError === void 0) { rethrowError = true; }
40 /**
41 * @internal
42 */
43 this._console = console;
44 this.rethrowError = rethrowError;
45 }
46 /**
47 * @param {?} error
48 * @return {?}
49 */
50 ErrorHandler.prototype.handleError = function (error) {
51 var /** @type {?} */ originalError = this._findOriginalError(error);
52 var /** @type {?} */ originalStack = this._findOriginalStack(error);
53 var /** @type {?} */ context = this._findContext(error);
54 this._console.error("EXCEPTION: " + this._extractMessage(error));
55 if (originalError) {
56 this._console.error("ORIGINAL EXCEPTION: " + this._extractMessage(originalError));
57 }
58 if (originalStack) {
59 this._console.error('ORIGINAL STACKTRACE:');
60 this._console.error(originalStack);
61 }
62 if (context) {
63 this._console.error('ERROR CONTEXT:');
64 this._console.error(context);
65 }
66 // We rethrow exceptions, so operations like 'bootstrap' will result in an error
67 // when an error happens. If we do not rethrow, bootstrap will always succeed.
68 if (this.rethrowError)
69 throw error;
70 };
71 /**
72 * \@internal
73 * @param {?} error
74 * @return {?}
75 */
76 ErrorHandler.prototype._extractMessage = function (error) {
77 return error instanceof Error ? error.message : error.toString();
78 };
79 /**
80 * \@internal
81 * @param {?} error
82 * @return {?}
83 */
84 ErrorHandler.prototype._findContext = function (error) {
85 if (error) {
86 return error.context ? error.context :
87 this._findContext(((error)).originalError);
88 }
89 return null;
90 };
91 /**
92 * \@internal
93 * @param {?} error
94 * @return {?}
95 */
96 ErrorHandler.prototype._findOriginalError = function (error) {
97 var /** @type {?} */ e = ((error)).originalError;
98 while (e && ((e)).originalError) {
99 e = ((e)).originalError;
100 }
101 return e;
102 };
103 /**
104 * \@internal
105 * @param {?} error
106 * @return {?}
107 */
108 ErrorHandler.prototype._findOriginalStack = function (error) {
109 if (!(error instanceof Error))
110 return null;
111 var /** @type {?} */ e = error;
112 var /** @type {?} */ stack = e.stack;
113 while (e instanceof Error && ((e)).originalError) {
114 e = ((e)).originalError;
115 if (e instanceof Error && e.stack) {
116 stack = e.stack;
117 }
118 }
119 return stack;
120 };
121 return ErrorHandler;
122}());
123function ErrorHandler_tsickle_Closure_declarations() {
124 /**
125 * \@internal
126 * @type {?}
127 */
128 ErrorHandler.prototype._console;
129 /**
130 * \@internal
131 * @type {?}
132 */
133 ErrorHandler.prototype.rethrowError;
134}
135//# sourceMappingURL=error_handler.js.map
\No newline at end of file