3

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!

3

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.

  • Thanks Peter . I'll check it out ! In the documentation I have read about aura:handler for aura:systemError event.The documentation clearly states "This event is automatically fired when an error is encountered during the run of a server-side action". Is it is something that can serve the purpose? Though it dint worked in my case ! – Monalisa Das May 29 '15 at 11:00
  • I've not tried using aura:handler with aura:systemError, to be honest. I know that some kind of Apex exception thrown on the server side will for sure cause an ERROR type callback like I've shown here. – pchittum May 29 '15 at 12:57
0

I don't Know whether it is a genuine solution or not but I am sharing a generic solution for this -

Apex class

public with sharing class ExceptionGenerator
{
  @AuraEnabled
  public static Object indexOutOfBoundsException()
  { 
     List<String> lst = new List<String>();
     System.debug(lst[0]);
  }
}

Aura Component

<aura:component controller="ExceptionGenerator"  implements="force:appHostable"> 
Click desired button <br /><br />
<ui:button label="Index out Of Bounds Exception" press="{!c.indexOutOfBounds}"/> 


Related JS

indexOutOfBounds : function(component)  
{
   var action = component.get("c.indexOutOfBoundsException"); 
    action.setCallback(this, function(response)  
    {
        var state = response.getState();
        if (state === "SUCCESS") 
           {
                var respVal = response.getReturnValue();
                if(respVal.message != 'undefined')
                {
                    console.log('response value : '+response.getReturnValue().message); 
                    // This error message can be set to set message in pageMessage
                }
                else
                {
                    // Your Normal functioning code
                }

           }   
    });
    $A.enqueueAction(action);
}

What I understood is You need to provide try catch in your apex class wherever there is possibility of exception and return the error message using generic return Type.This value then can be used to display message at user's end which is somewhat more informative unlike a default message like "An internal server error has occurred Error ID: 670155758-10800 (-1177185898)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.