Catch Angular 2 exceptions

If Angular 2 encounters an internal exception, it will not be registered on the console.

How can I detect exceptions like the following?

EXCEPTION: Error during instantiation of MainFormComponent!. ORIGINAL EXCEPTION: TypeError: Cannot set property 'crashMeMaybe' of undefined ORIGINAL STACKTRACE: ... stacktrace ... ERROR CONTEXT: ... context object ... 

Are there any subscriptions available? Where is such documentation?

+4
exception exception-handling angular
Aug 11 '15 at 15:24
source share
1 answer

There is an ExceptionHandler interface through which you can provide a custom implementation of your choice. See here and the documentation .

From BETA.0 sources: (facade / exception_handler.d.ts)

 /** * Provides a hook for centralized exception handling. * * The default implementation of `ExceptionHandler` prints error messages to the `Console`. To * intercept error handling, * write a custom exception handler that replaces this default as appropriate for your app. * * ### Example * * ```javascript * * class MyExceptionHandler implements ExceptionHandler { * call(error, stackTrace = null, reason = null) { * // do something with the exception * } * } * * bootstrap(MyApp, [provide(ExceptionHandler, {useClass: MyExceptionHandler})]) * * ``` */ 

UPDATED:

I had a little problem with BETA.0

  • import had to be hacked,
  • the interface seems to be dirty with internal details.

I ran it as:

 import {ExceptionHandler} from 'angular2/src/facade/exception_handler'; export interface IExceptionHandler { call(exception: any, stackTrace?: any, reason?: string): void; } export class CustomExceptionHandler implements IExceptionHandler { call(exception: any, stackTrace: any, reason: string): void { alert(exception); } } bootstrap(DemoApp, [ new Provider(ExceptionHandler, { useClass: CustomExceptionHandler }) ]); 
+8
Dec 18 '15 at 12:41
source share



All Articles