From Apex class:
From client-side controller:
@AuraEnabled
public static List<Account> fetchAccount() {
throw new AuraHandledException('User-defined error');
}
From client-side controller:
doInit: function(component, event, helper) {
var action = component.get('c.fetchAccount');
action.setParams({ firstName : cmp.get("v.firstName") });
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
component.set('v.Accounts', response.getReturnValue());
}
else if (component.isValid() && state === "ERROR") {
console.log("Error Message: ", response.getError()[0].message);
}
});
$A.enqueueAction(action);
}