Customizing the angular2 exception handler


#1

Anybody have an example of how to install a custom exception handler?

I’ve been looking into wiring this up:
https://angular.io/docs/js/latest/api/core/ExceptionHandler-class.html

The example code throws compile errors in my project.


#2

Anyone thoughts on this?


#3

Trying another bump…

Could we add a hook for ExceptionHandler here? Or is there an other to catch all exceptions?

https://github.com/driftyco/ionic/blob/2.0/ionic/config/bootstrap.ts


#4

You can define providers inside the @App annotation:

@App({
  providers: [provide(ExceptionHandler, {useClass:MyExceptionHandler})]
})

#5

I did that and it worked, thanks.

Typescript still complains with the override documented [at the link at the top of the page]
(https://angular.io/docs/js/latest/api/core/ExceptionHandler-class.html47), even though it works. One way to make Typescript stop complaining is to use extends instead of implements - that worked without typescript type complaints. Define this before class App in app.ts:

class MyExceptionHandler extends ExceptionHandler {  
    call(error, stackTrace = null, reason = null) {
        // do something with the exception
    }
}

Then add the providers line in the response above this one to your app class and this should work fine.

However: doing all the above works on catching many exceptions, still, some exceptions do not get caught using this, not sure yet why…