0

I am working on a component+Apex class.

I need to throw an error message via AuraHandledException.

Everything seems to be working fine except for the error message itself: it's showing up with an extra word ("Exception") at the beginning of the message!

enter image description here

I really do not know what to do. That extra "Exception" must be coming from some default value that I do not recognise. Any help would be appreciated.

Here is my code for reference.

My Apex code is the following:

      if(acc.Login_Email__c != null || acc.Login_Email__c != ''){
        List <Account> loginEmail = [SELECT ID FROM Account WHERE Login_Email__c =: acc.Login_Email__c];
            if(loginEmail.size()>0){
                AuraHandledException e = new AuraHandledException('A customer with this email address already exists');
                e.setMessage('A customer with this email address already exists');
                throw e;
            }
        }

and my JavaScript controller has the following:

else if(state === 'ERROR'){

        var errors = action.getError()[0].message;
        var errorsMsg = [];
        errorsMsg.push(errors);
        cmp.set("v.errors", errorsMsg);
        helper.hideSpinner(cmp);
    }    

1 Answer 1

0

Try this one

.cls

throw new AuraHandledException('A customer with this email address already exists');

.js controller

else if (state === 'ERROR') {
   var errors = response.getError();
   if(errors) {
      if (errors[0] && errors[0].message) {
         var errorsMsg = [];
         errorsMsg.push(errors[0].message);
         cmp.set("v.errors", errorsMsg);
         helper.hideSpinner(cmp);

         console.log(errors[0].message);
      }
   }else {
      console.log("Unknown error");
   }
}

check this out

To be more accurate I tried testing it like so and it worked fine:

else if (state === 'ERROR') {
   var errors = response.getError();
   if(errors) {
      if (errors[0] && errors[0].message) {
         var resultsToast = $A.get("e.force:showToast");
         resultsToast.setParams({
             "type":"error",
             "title": errors[0].message,
             "message": errors                        
         });
         resultsToast.fire();

         console.log(errors[0].message);
      }
   }else {
      console.log("Unknown error");
   }
}

I am not sure how you handle the code in html to display the error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .