ToasController in custom error handler


#1

I would like to show toast msgs on errors globally but cant seem to find a way to have access to the toastcontroller in custom error handler. I would like access to loadingcontroller as well here but one thing at a time i guess,

Here is my code

class CustomErrorHandler implements ErrorHandler {
   constructor(private toastCtrl: ToastController) {

   }
  handleError(err: any): void {
  console.log("error caught globally", err);

 } 

}

having this i get a build error of

Runtime Error
Can’t resolve all parameters for CustomErrorHandler: (?).

I have
import { LoadingController, ToastController } from 'ionic-angular'; at the top of my app.module.ts where the error handler class resides.


#2

You have to make the above CustomErrorHandler class Injectable since there are parameters inside the constructor.

  1. Add this import line at top of that file:
    import { Injectable } from ‘@angular/core’;

  2. Add the word ‘@Injectable’ in the line above the class declartion like below:
    @Injectable
    class CustomErrorHandler implements ErrorHandler

This will solve your error.


#3

Can you try this one instead if it fits your needs:

import { Toast } from ‘@ionic-native/toast’;

From: https://ionicframework.com/docs/native/toast/