Skip to content

Instantly share code, notes, and snippets.

@mshanemc
Last active February 25, 2021 13:54
Show Gist options
  • Save mshanemc/238eca4c4ac5b5d76d844106e9c9f3d8 to your computer and use it in GitHub Desktop.
Save mshanemc/238eca4c4ac5b5d76d844106e9c9f3d8 to your computer and use it in GitHub Desktop.
Example of Lightning Components for handling Apex Callback Errors
action.setCallback(this, function(a){
if (a.getState() === "SUCCESS") {
//happy path stuff
} else if (a.getState() === "ERROR"){
var appEvent = $A.get("e.c:handleCallbackError");
appEvent.setParams({
"errors" : a.getError(),
"errorComponentName" : "someUniqueName"
});
appEvent.fire();
}
});
$A.enqueueAction(doUpdate);
<aura:component controller="myApex">
...
<!--whichever component is going to throw an apex callback error needs to register to use the error event-->
<aura:registerEvent name="handleCallbackError" type="c:handleCallbackError"/>
<!--add the errorHandler anywhere in the component, preferably at the outermost level-->
<c:LightningErrorHandler errorHandlerName="someUniqueName"/>
...
</aura:component/>
<aura:component >
<aura:attribute name="errorHandlerName" type="string" required="false" description="if set, only toasts errors when the name matches" access="global"/>
<aura:handler event="c:handleCallbackError" action="{!c.makeToast}" />
</aura:component>
({
makeToast : function(component, event, helper) {
//should we skip this event?
if (event.getParam("errorComponentName") && (component.get("v.errorHandlerName") !== event.getParam("errorComponentName"))){
return;
}
var errors = event.getParam("errors");
if (errors) {
errors.forEach( function (error){
//top-level error. there can be only one
if (error.message){
helper.toastThis(error.message);
}
//page-level errors (validation rules, etc)
if (error.pageErrors){
error.pageErrors.forEach( function(pageError) {
helper.toastThis(pageError.message)
});
}
if (error.fieldErrors){
//field specific errors--we'll say what the field is
for (var fieldName in error.fieldErrors) {
//each field could have multiple errors
error.fieldErrors[fieldName].forEach( function (errorList){
helper.toastThis(errorList.message, "Field Error on " + fieldName + " : ")
});
}; //end of field errors forLoop
} //end of fieldErrors if
}); //end Errors forEach
}
}
})
({
toastThis : function(message, title) {
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": title || "Error:",
"message": message,
"type": "error",
"mode": "sticky"
});
toastEvent.fire();
}
})
@martyychang
Copy link

Do you by chance have code that shows how to throw or handle an exception in Apex, so that the resulting message shows up in action.getError()[0].pageErrors or some other predictable property?

@lekkimworld
Copy link

I like to throw exceptions from Apex as AuraHandledException like below
try { // do stuff } catch (DomainSpecificException e) { throw new AuraHandledException('Some error message making sense for the receiving controller'); }

@mshanemc
Copy link
Author

somehow this got some attention on twitter. If you're reading this recently, use https://github.com/mshanemc/lightningErrorHandler instead. It's got some more useful features built in.

@KrupeshShivaramaiah
Copy link

HI,
I am using this component to throw error message from Apex callbacks.
But My Component is embedded in a Visualforce page.
Will the toast still show up ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment