The communication between Lightning Components and Apex can at first seem a little unwieldy when compared to the close coupling between Apex and Visualforce.
With a little coaxing and massaging however, Apex can be brought to obey your beautiful Lightning stylings even when it throws a nasty exception. I found myself frustrated by the murky handling of errors in my try — catch blocks at first. That was until I discovered the beauty of throw new AuraHandledException()!
I had already wired up my modal to let the user know their query had returned no data but until discovery of the AuraHandledException I was not finding a friendly way to insert the likes of callout errors (500, 400, etc) into my pretty modal. I guess I should not have been surprised when throw new AuraHandledException() did exactly what it said it did! In my data handling helper I was already trying to catch the error in the response but because my errors were unhandled they were creating those ugly red Lightning error pop-ups generated by the system.
The code below is not a complete implementation but is intended to give you a sense of the important parts which are:
1. In your try – catch block on your server call use throw new AuraHandledException()!
2. Wherever you are processing your response, in my case the FetchDataHelper handle your error by passing it to your display prompt/modal.
3. I am creating a modal dynamically in the parent component, the dataDisplayHelper creates the modal with the appropriate error. Credit to Mike Topalovich for the modal code(not included) and the dataFetch code!. You can find lots of examples online for modal creation — it is fairly straightforward.
4. Ultimately what you end up with is graceful error handling which makes your users happy and not stressed out by your application. Also, it makes troubleshooting much friendlier should someone log a defect (and hopefully add a screenshot) after you go to production…. that never happens.
I have been developing with the SFDC platform for several years. In this time I have surmounted many a learning curve and devised copious work arounds for dead ends that I have run into because of system limitations. These challenges have been exciting to overcome but have often meant adding code that I knew I would one day be back to refactor. Salesforce has been sold as a clicks not code development platform but as anyone who has implemented SFDC knows there is a limit to where configuration alone can take you. I think this has created divisions between admins and developers. There has not been until now, a way to handoff your beautifully written and toiled over code solutions to your admin wizard friends. Along came Lightning and everything changes…
Adoption is still low because so many users are married to their Classic UI and are not inclined to invest in making the switch or are not sure where to begin. I think that Lightning adoption will grow as more developers understand the power of the framework. I am taking the Dev 601 course this week with Mike Topalovich. Although I have been building some Lightning Components over the past few months, I am finding lots of good nuggets that I have not yet discovered in this training. Kudos to Mike for his depth of knowledge on this subject and for keeping some levity through the tech talk!
Here are some of my favorite learnings so far:
- Components are your lego blocks… developing with SFDC is now modular by design!
- <Lightning:layout> component gives you the Grid for free! Crowds go wild… especially those who have toiled over SLDS styling for days….
- <aura:if> is magical…. I can see use cases with this involving custom component configuration by admins.
- Expose your components for use by your Admin friends and make them configurable by adding a Design Resource
- LEX is still a work in progress but there is something special about the SFDC developer community…. (just look at StackOverflow if you don’t believe me) I know that the innovation of folks working with SFDC LEX will grow exponentially now that we have just been given this awesome new set of tools for our toolkit!
….More to come!