3

We have an error log object where we store the errors and I have a simple try catch like below... My issue is that the insert in the finally does not appear to be happening. Any advice? or is that a limitation in the try-catch error handling?

list<Error_Log__c> error_logs = new list<Error_Log__c> ()
try {
   if(status__c == '100') {
      \\do something
   } else {
       throw new AuraHandledException('System Error');
   }  
} Catch (Exception e) {
   Error_Log__c rec = new Error_Log__c(Name="Error Message: ");
   error_logs.add(rec);
   throw new AuraHandledException('System Error');
} finally {
  insert error_logs;
} 

3 Answers 3

6

Since all DML is rolled back on an unhandled exception, my typical solution is to instead throw the error back to the client, then have the client log the exception in a separate call. We have "unlimited" calls in LWC, so there's no need to be ultra-conservative. You just need a separate method:

@AuraEnabled(cacheable=false) public static logError(Error_Log__c log) {
  insert log;
}

You, nor your users, will notice the few extra milliseconds it takes to make a separate call, in most cases.

In your code, the finally code is executed, but the results are rolled back anyways after the finally completes, because it's still in the same transaction where the exception was thrown.

5

If you allow an exception to propagate unhandled out of your code, which you do at the end of your catch block, you will cause the Salesforce platform to treat the session as failed and therefore all DML will be rolled back. That means even the error log insert will be undone.

The first option is to change how you notify your component about the error. Alternatively you ensure the error logs are handled in a separate session.

One option for the latter is to package your error details into a "publish immediately" platform event that you publish on the event bus in your finally. Being publish immediately, this event operates outside the current session and will not be rolled back because of the session failing.

You can then implement a trigger-based platform event subscriber that converts the platform event into an error log record and performs a DML insert. This DML runs in a separate session and thus can still succeed despite the original session failure.

2
  • Ok.. I just need to be able to throw the AuraExceptionHandling and also be able to log the error log records.
    – jojopogi
    Commented Feb 11, 2021 at 20:26
  • So you either change how to notify the component of a failure or you split the session into two (so to speak) as described.
    – Phil W
    Commented Feb 11, 2021 at 20:29
0

So you are trying to throw an exception in the catch block. This won't work since the new throw is not in the try block directly, which means it will be thrown up to the next try-catch in the hierarchy.

2
  • So if I don't have a throw an exception in the catch, then my lightning component will not get the "error" state in the component. I just need my aura component to know there was a server side aura exception and also log the error logs.
    – jojopogi
    Commented Feb 11, 2021 at 20:27
  • 1
    Move the status code check outside of the try...catch block and throw the exception there.
    – Layne B
    Commented Feb 12, 2021 at 6:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .