1

I have apex class with custom error message and if there is any error occurred then I would like to show to the user on the UI.

Apex class code //snippet

     try {
         //some awesome code          
     } catch(Exception e){ 
        AuraHandledException ex = new AuraHandledException('');
        system.debug('error: ' + e.getMessage()); //I can see the error in the system.debug log.
        ex.setMessage(e.getMessage());
        throw ex;
     }

But on the Aura Component the error message is getting swallowed

     .....
     else if (state === "ERROR") {              
            var errors = response.getError();  
            if(errors){
                if(errors.message){
                    alert(JSON.stringify(errors.message)); 
                }
                else{
                    alert('nothing to show);
                }    
            }
        }

I'm getting errors.message empty string but I know that apex is throwing an error message but somehow it's getting swallowed.

1

May be not the best way to do it. But I used to have an Apex wrapper for this purpose, and handle the error in Apex try-catch and then use it in front-end as needed.

E.g. Apex class

@AuraEnabled
public static CmpDataWrapper saveData(String valTest) {
    CmpDataWrapper idw = new CmpDataWrapper();
    
    try{
        // Do stuff 
    }
    catch(Exception ex){
      idw.hasError = true; 
      idw.message = ex.getLineNumber() +' - '+ex.getMessage();
      System.debug(idw);
    }
    
    return idw;
}

    // Component data wrapper to keep all data records, settings, config retrieved from Backend
    public class CmpDataWrapper{
        /**
        * @description List of Contact Wrappers
        */
        @AuraEnabled
        public List<ConWrapper> listConW{get;set;}
        
        /**
        * @description User message (if any)
        */
        @AuraEnabled
        public String message{get;set;}
        
        /**
        * @description if object has any errors
        */
        @AuraEnabled
        public Boolean hasError{get;set;}
    }

Aura Helper:

saveData: function (component, event) {

    let action = component.get('c.saveData');
    
    action.setParams({
        'valTest' : component.get('v.valTest'),
    });
    
    action.setCallback(this, function(response){            
        
        let state = response.getState();
        
        //Success
        if (component.isValid() && state === "SUCCESS") {
            let ret = response.getReturnValue();

            if (ret.hasError==true)
                helper.showMessage2User(component, helper, $A.get("$Label.c.Error"), ret.message,'error','sticky');
            else{
                component.set('v.showModal', false);                     
                helper.showMessage2User(component, helper, $A.get("$Label.c.Success"),ret.message ,'success','');
            }

            //Unknown Error
        } else if (state === "ERROR") {
            var errors = response.getError();
            helper.showMessage2User(component, helper, $A.get("$Label.c.Error"), errors[0].message,'error','sticky');
        }
        
    });
    
    $A.enqueueAction(action); // Run the action
},



showMessage2User: function(component, helper, title, message, type, mode){
    var toastEvent = $A.get("e.force:showToast");
    if(toastEvent){
        toastEvent.setParams({
            "title": title,
            "message": message,
            "type": type,
            "mode": mode
        });
        
        toastEvent.fire();
    } else {
        alert(message);
    }  
},
1

It's errors[0].message. Despite the singular term getError(), it actually returns a list of errors. See the example in the docs.

        else if (state === "ERROR") {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }

Also note that, apparently, there may be an error state without errors, but this is abnormal in my experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.