Below are the steps for doing exception handling in Salesforce lightning
1) Add exception message in apex controller to AuraHandledException class so that these message can be accessed in lightning component. If we use normal exception class to throw error messages from apex controller to lightning component, it is not possible. We have to use AuraHandledException class.
This AuraHandledException class can be used in controller or service layer of the apex enterprise patterns.
For Example :
Apex Controller
try{
}
catch(Exception e){
AuraHandledException auraExceptionObj = new AuraHandledException(e.getMessage());
throw auraExceptionObj;
}
Lightning Component
var myAction = component.get("c.myMethod");
myAction.setParams({
"rec": component.get("v.Account")
});
myAction.setCallback(this,function(response){
if(component.isValid() && response.getState() === "SUCCESS"){
}else{
var errors = response.getError();
console.log(errors[0].message);
component.set("v.stdExceptionMsg",errors[0].message);
}
});
$A.enqueueAction(myAction);
In the above code errors[0].message will fetch the error message from the AuraHandledException class.
No comments:
Post a Comment