| 
									
										
										
										
											2016-06-23 09:47:54 -07: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 13:49:16 -08:00
										 |  |  | import {getDebugContext, getErrorLogger, getOriginalError} from './errors'; | 
					
						
							| 
									
										
										
										
											2017-03-14 09:16:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-27 13:19:00 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-16 14:35:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 22:47:11 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-04-05 10:16:31 +01:00
										 |  |  |  * Provides a hook for centralized exception handling. | 
					
						
							| 
									
										
										
										
											2015-04-13 21:00:52 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-09-23 15:05:43 -07: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-13 21:00:52 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-05-18 16:13:00 +01:00
										 |  |  |  * @usageNotes | 
					
						
							| 
									
										
										
										
											2016-09-23 15:05:43 -07:00
										 |  |  |  * ### Example | 
					
						
							| 
									
										
										
										
											2015-04-13 21:00:52 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-09-23 15:05:43 -07:00
										 |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  |  * class MyErrorHandler implements ErrorHandler { | 
					
						
							| 
									
										
										
										
											2016-09-23 15:05:43 -07:00
										 |  |  |  *   handleError(error) { | 
					
						
							| 
									
										
										
										
											2015-04-13 21:00:52 -07:00
										 |  |  |  *     // do something with the exception
 | 
					
						
							|  |  |  |  *   } | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-08-16 11:15:01 -07:00
										 |  |  |  * @NgModule({ | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |  *   providers: [{provide: ErrorHandler, useClass: MyErrorHandler}] | 
					
						
							| 
									
										
										
										
											2016-08-16 11:15:01 -07:00
										 |  |  |  * }) | 
					
						
							|  |  |  |  * class MyModule {} | 
					
						
							| 
									
										
										
										
											2015-04-13 21:00:52 -07:00
										 |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2018-10-19 16:27:04 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											2015-03-31 22:47:11 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  | export class ErrorHandler { | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * @internal | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   _console: Console = console; | 
					
						
							| 
									
										
										
										
											2015-07-22 17:13:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |   handleError(error: any): void { | 
					
						
							| 
									
										
										
										
											2017-03-14 09:16:15 -07: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 17:13:42 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-09 17:21:25 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |   _findContext(error: any): any { | 
					
						
							|  |  |  |     if (error) { | 
					
						
							| 
									
										
										
										
											2017-01-27 13:19:00 -08:00
										 |  |  |       return getDebugContext(error) ? getDebugContext(error) : | 
					
						
							|  |  |  |                                       this._findContext(getOriginalError(error)); | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-23 15:05:43 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return null; | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-09 17:21:25 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2017-01-27 13:19:00 -08:00
										 |  |  |   _findOriginalError(error: Error): any { | 
					
						
							|  |  |  |     let e = getOriginalError(error); | 
					
						
							|  |  |  |     while (e && getOriginalError(e)) { | 
					
						
							|  |  |  |       e = getOriginalError(e); | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return e; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-16 14:35:27 +01:00
										 |  |  | } |