Extend IonicErrorHandler + navigate to new page


#1

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?


Add custom error handler to prevent app from being broken
#2

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;

#3

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?


#4

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.


#5

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