2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-01-09 16:49:16 -05:00
|
|
|
import {getDebugContext, getErrorLogger, getOriginalError} from './errors';
|
2017-03-14 12:16:15 -04:00
|
|
|
|
2017-01-27 16:19:00 -05:00
|
|
|
|
2015-02-16 08:35:27 -05:00
|
|
|
|
2015-03-31 18:47:11 -04:00
|
|
|
/**
|
2018-04-05 05:16:31 -04:00
|
|
|
* Provides a hook for centralized exception handling.
|
2015-04-14 00:00:52 -04:00
|
|
|
*
|
2016-09-23 18:05:43 -04:00
|
|
|
* The default implementation of `ErrorHandler` prints error messages to the `console`. To
|
|
|
|
* intercept error handling, write a custom exception handler that replaces this default as
|
|
|
|
* appropriate for your app.
|
2015-04-14 00:00:52 -04:00
|
|
|
*
|
2018-05-18 11:13:00 -04:00
|
|
|
* @usageNotes
|
2016-09-23 18:05:43 -04:00
|
|
|
* ### Example
|
2015-04-14 00:00:52 -04:00
|
|
|
*
|
2016-09-23 18:05:43 -04:00
|
|
|
* ```
|
2016-08-30 21:07:40 -04:00
|
|
|
* class MyErrorHandler implements ErrorHandler {
|
2016-09-23 18:05:43 -04:00
|
|
|
* handleError(error) {
|
2015-04-14 00:00:52 -04:00
|
|
|
* // do something with the exception
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-08-16 14:15:01 -04:00
|
|
|
* @NgModule({
|
2016-08-25 03:50:16 -04:00
|
|
|
* providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
|
2016-08-16 14:15:01 -04:00
|
|
|
* })
|
|
|
|
* class MyModule {}
|
2015-04-14 00:00:52 -04:00
|
|
|
* ```
|
2018-10-19 11:27:04 -04:00
|
|
|
*
|
|
|
|
* @publicApi
|
2015-03-31 18:47:11 -04:00
|
|
|
*/
|
2016-08-25 03:50:16 -04:00
|
|
|
export class ErrorHandler {
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_console: Console = console;
|
2015-07-22 20:13:42 -04:00
|
|
|
|
2016-08-25 03:50:16 -04:00
|
|
|
handleError(error: any): void {
|
2017-03-14 12:16:15 -04:00
|
|
|
const originalError = this._findOriginalError(error);
|
|
|
|
const context = this._findContext(error);
|
|
|
|
// Note: Browser consoles show the place from where console.error was called.
|
|
|
|
// We can use this to give users additional information about the error.
|
|
|
|
const errorLogger = getErrorLogger(error);
|
|
|
|
|
|
|
|
errorLogger(this._console, `ERROR`, error);
|
|
|
|
if (originalError) {
|
|
|
|
errorLogger(this._console, `ORIGINAL ERROR`, originalError);
|
|
|
|
}
|
|
|
|
if (context) {
|
|
|
|
errorLogger(this._console, 'ERROR CONTEXT', context);
|
2015-07-22 20:13:42 -04:00
|
|
|
}
|
2015-07-23 21:00:19 -04:00
|
|
|
}
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-08-25 03:50:16 -04:00
|
|
|
_findContext(error: any): any {
|
|
|
|
if (error) {
|
2017-01-27 16:19:00 -05:00
|
|
|
return getDebugContext(error) ? getDebugContext(error) :
|
|
|
|
this._findContext(getOriginalError(error));
|
2015-07-23 21:00:19 -04:00
|
|
|
}
|
2016-09-23 18:05:43 -04:00
|
|
|
|
|
|
|
return null;
|
2015-07-23 21:00:19 -04:00
|
|
|
}
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2017-01-27 16:19:00 -05:00
|
|
|
_findOriginalError(error: Error): any {
|
|
|
|
let e = getOriginalError(error);
|
|
|
|
while (e && getOriginalError(e)) {
|
|
|
|
e = getOriginalError(e);
|
2015-07-23 21:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
2015-02-16 08:35:27 -05:00
|
|
|
}
|