Showing error message in alert controller

Hi Experts,

I want to display my error message which is part of catch block in alter controller. My catch block is able to catch the error message but i am not able to prompt it to the end user in the form of an alert. Please suggest right way to address this.

Appreciate your help in advance.

Cheers

Code:

catch((error) => {
        let alert = this.alertCtrl.create({
          title: 'error',
          message: reject(error),
          //subTitle: error,
          buttons: [
        {
          text: "Ok",
          handler: data => {
            console.log("Ok Clicked")
          }
        }]
        });
        alert.present();
        reject(error);
      });

First, that reads terribly – you’re setting yourself up for typo hell. Pull the alert out into a separate method, like:

catch( (error) => this.displayErrorAlert(error) )

Second, the message property takes a string. So you need to understand the model of the error you’re handling. Some errors have a code property, so you could just pass that if your error class uses error.code, or if you want a more user-friendly version, you could if-then on the code string, and turn it into a complete non-computer-sounding sentence.

Thanks for the quick revert. I basically want to popup error message without any change so i believe i will have to provide alert controller inside catch block where i will be passing the error code.

I am little new in java script hence my code might look little distorted. Would it be possible for you to share a code snippet which can be used for the requirement.

I won’t provide further code, because I provided some already, and you incorrectly said it wouldn’t work in your situation. My recommendation at this point is that you learn the basics of TypeScript before proceeding. Best wishes.

1 Like

No. I never said it wouldn’t work in my situation. I was just explaining the issues once again. Never mind. Will follow your advice. Thanks for the revert.

Additionally, the fact that I see calls to reject suggest that you’re explicitly instantiating promises, which you almost certainly don’t really want to be doing.

1 Like