Last active
February 25, 2021 13:54
-
-
Save mshanemc/238eca4c4ac5b5d76d844106e9c9f3d8 to your computer and use it in GitHub Desktop.
Example of Lightning Components for handling Apex Callback Errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
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 | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
toastThis : function(message, title) { | |
var toastEvent = $A.get("e.force:showToast"); | |
toastEvent.setParams({ | |
"title": title || "Error:", | |
"message": message, | |
"type": "error", | |
"mode": "sticky" | |
}); | |
toastEvent.fire(); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?