Extend IonicErrorHandler + navigate to new page

I understand that Ionic implements Angular’s ErrorHandler via IonicErrorHandler, and we can extend (or replace) the use of that.

When an unhandled exception happens, I’d like to navigate to a specific support page, passing this error in for display. It seems like that’s not possible, because getting a NavController in this Error Handler class isn’t possible?

To answer my own question, I ended up using events to pull this off.

In custom error handler

this.events.publish(Constants.EVENT_ERROR_UNHANDLED, err);

In app.components.ts

this.events.subscribe(Constants.EVENT_ERROR_UNHANDLED, (error) =>{
    this.nav.setRoot(SupportPage, {err: error});
});

In support page

constructor( ...<snip!>... public navParams: NavParams) {
    this.unhandledError = this.navParams.data.err;

Interesting idea here. I assume this works even if Ionic is barfed? Meaning that you can use the router if the system is in a really bad state?

I’m not sure about that. If Ionic is really messed up, theoretically the unhandled event may not even fire - you may have an app crash on your hands. If the unhandled event is firing, then ideally things are semi-stable and the navigation would still work. In my case this was just protection against accidentally not handling a catch somewhere, not trying to protect against crashes.

Ok, that make sense. Thanks for sharing this approach!