function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kania @DaydreamKania @Daydream 

AuraHandledException and Logging Error

I am calling a server side action from a Lightning Component. When an error occurs I am trying to log them in a custom object using an @future method and then throwing an AuraHandledException so the component can display the message. It seems like this won't work with AuraHandledException since it causes a Script-thrown execption in the logs which causes the @future method not to execute. Am I doing something wrong or is there another way to accomplish it?

Here is documentation I am referencing:

Error Handling Best Practices for Lightning and Apex:
https://developer.salesforce.com/blogs/2017/09/error-handling-best-practices-lightning-apex.html

An Introduction to Exception Handling:
https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling
 
@auraEnabled
public static void getSomething(){
    
    try{
        //Do Something here 
        
    }catch(exception e){
        
        //@future - log error in custom object
        futureCreateErrorLog.createErrorRecord(e.getMessage());
        
        throw new AuraHandledException('Error message to display in component');
    }
    
}

User-added image
Pablo Gonzalez Alvarez 3Pablo Gonzalez Alvarez 3
Don't throw an exception. You can simply capture the exception and display a toast message with the error message...there's no need to throw the script exception. 
Kania @DaydreamKania @Daydream
Its something I have considered but want to see if someone has a better idea. AuraHandledException is the recommended method to handle and pass errors to the component. I would prefer not have to create a Wrapper Class for error handling since Saleforce provides a mechinism.
Pramodh KumarPramodh Kumar
Salesforce provided log object to store the Exceptions

Here is my usage of that log object
({
	loadSobjects : function(component) {
        var action = component.get("c.fetchRecords");
        action.setParams({ strQuery : component.get("v.query")});
        action.setCallback(this, function(response) {
            var state = response.getState();
            
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
				component.set("v.lstSobject", result.lstSobject);
                component.set("v.lstFieldApi", result.lstFieldApi);
                component.set("v.lstFieldLabel", result.lstFieldLabel);
                
                setTimeout(function(){ $('#example').DataTable(); }, 500);
            }
            else if (state === "ERROR") {
                $A.log("Errors", a.getError());
            }
        });
        $A.enqueueAction(action);
	}
})


Thanks
Pramodh
allaboutlightning.com

 
Kania @DaydreamKania @Daydream
$A.log(), logs the error in the javascript console. I am looking to store the error in a custom object for auditing and troubleshooting purposes. 
Pablo Gonzalez Alvarez 3Pablo Gonzalez Alvarez 3
Have you tried sending an application event a letting another component log the error? You can create a mini component whose only responsible for logging errors.
Saravanan Gengan 9Saravanan Gengan 9
Please try something like this. 
private static AuraHandledException createAuraHandledException(String methodName,String Message){
        AuraHandledException e = new AuraHandledException(Message);
        e.setMessage(Message);
        system.debug(methodName + e);
        return e; 
    }

and then 
 
throw createAuraHandledException('methodname', e.getMessage());

 
Ravi Dutt SharmaRavi Dutt Sharma
Try changing the order, log the exception first by calling the future method and then throw the AuraHandledException.
Shaun HawleyShaun Hawley
Has anyone come up with a good solution to this. I am attempting to do the same thing. I need to log these errors for audit and monitoring. We also like to show the error ID to the user. That way when we get error reports we can look up a specific error record. The aura handled exception seems to be preventing this by rolling back all the transactions. Does anyone have suggestions?
利康 鍬田利康 鍬田
I have some ideas as a solution to this problem.

The way to create a custom error log record with AuraHandledException.
I recommend "No 4" method.

1. use Queueable Interface 

2. Publish error topics with "Plattform Event", and  subscribe to topics with "Process Builder"

3. Do not use "AuraHandledException" when catching Exception,
   return a custom wrapper class and display the error message in LightningComponentController.js.


4.  Use "AuraHandledException" and The response of state will be "ERROR" In LightningComponentController.js,
     fire cusom error event and inserts custom object log records. 


I tried to use the "No.1" and JobId was created in System.debug, but it got rolled back.

The method of "No.2" is under trial and no result has come out,
but I guess it is probably rolled back.

The method of "No 3" occures time and effort,
it is expected that an error log will be created unless throwing Exception at the last processing.