[SalesForce] How to handle exception in Lightning Components

I am new to Salesforce Lightning Components.

I have an Apex class that returns map of task's owner name as key and list of task as value and it is called from controller of some lightning component.When user does not have accessibility to any of the fields of task Object and at that time it throws NULL POINTER EXCEPTION as per my code design.

I want to add custom page message stating "this particular field(field name) is not accessible by the current user". I can not use ApexPages.addMessage in my catch block since that is pertaining to Visualforce page.

I can not find anything specific in salesforce documentation for Exception handling for lightning components.

Please help. Thanks in advance!

Best Answer

Broadly speaking, you'll need to handle the error response in the callback of your action and then bubble it up to the UI somehow.

Here is a very rudimentary example of this for Salesforce1 Mobile:

action.setCallback(this, function(resp) {
    attachId = resp.getReturnValue();

    var showToast = $A.get('e.force:showToast');
    showToast.setParams(
        {
            'title': 'Progress: ',
            'message': resp.state
        }
    );
    showToast.fire();
}, 'ALL');

The trick is that the parameter in the anonymous callback function has the state attribute. In my example above, I'm just using it as the message I send to the user. It will either say SUCCESS or ERROR.

Not very useful.

More useful might be to use it in an if and have some code execute when an error occurs so you can write to the console, and have a more intelligent message for the user.

if (resp.state === 'ERROR') {
  //...do something here
}

In my case, I'm communicating to the user with the showToast event. This event will pop down a toast message in the Salesforce1 mobile app. This event also has a duration attribute that can be used to make the message last longer.

If you are not using S1 mobile, or don't want the notification to auto-close, there is a ui:message component that is in the docs. I've not played with it yet, but it has a promising attribute called closable in it.

One last thing...you might want to have a different callback for error versus success. Ostensibly you would do this if you want to encapsulate your error logic in one callback (in a helper, for instance) that you use in several different places in your component.

Happily, the JS api supports this in the setCallback function.

To register a callback for success-only responses:

action.setCallback(this, function(resp){
  //...my success callback code goes here. 

}, 'SUCCESS');

To register a callback for error-only conditions:

action.setCallback(this, function(resp){
  //...my error callback code goes here. 

}, 'ERROR');

These features are documented in the JS Api docs and are worth spending some time just playing with to understand better.

Long story short, you can't just do this in your Apex code. You will need to do some work in your Lightning Component Javascript in order that you communicate error conditions to users.