0

we have this code in catch block, the requirement is to capture the error before throwing the error to lwc component in a field.

Repair_Repaired_Vehicle_Cost__c cost = new Repair_Repaired_Vehicle_Cost__c( 
    Id = String.valueOf(apinameToObject.get('id')),
    Rule_Failed_for_debug__c = 'Record transfer failed due to the repair criteria: ' + criteriaId + ' Formula:' + criteriaExpression + 'Error Message ' +e.getMessage()
);

update cost;

throw new AuraHandledException('Record transfer failed due to the repair criteria: ' +criteriaId +' Formula:' +criteriaExpression +'Error Message ' +e.getMessage());

But the value is not reflected in the field, FLS is not read-only, checked the debug in trigger the value is coming, but still not able to see the value.

I think i am doing some silly mistake in updating the record but not getting it.

2
  • 1
    Why are you throwing an exception after updating the record? Doing that should cause the entire transaction to be rolled back (thus undoing the update you're trying to make).
    – Derek F
    Dec 9, 2021 at 17:15
  • i just want to capture the error, this method is used in lwc controller. Dec 9, 2021 at 17:17

1 Answer 1

4

An uncaught exception will roll back almost all database transactions, with the single exception of Platform Events that are set to Deliver Immediately. You have two basic options here.

Global Error Handler/Client-Side Logging

You can use a global error handler to catch any errors and log them, or you can make a separate server call in your catch blocks:

// Sub-Option 1
async errorCallback(error, stack) {
  await logServerError({ error, stack }) // This would be an Apex method
}
// Sub-Option 2
try {
  let result = await myApexMethod({ params })
} catch(error) {
  await logError({error}) // A separate Apex method to log errors
}

This allows you to catch even governor limit messages.

Platform Event

A Platform Event set to "Deliver Immediately" will persist through a thrown, uncaught exception. You can set a trigger on this to perform logging at that time.

// This is Apex code
Logger__e log = new Logger__e(Message__c='blah blah blah');
EventBus.publish(log);
throw new AuraHandledException('blah blah blah');
1
  • With the first approach, there is a chance that user can see what the error is, if he can open console and look at network tab and all that. Although that might not be a big concern many times.
    – javanoob
    Dec 11, 2021 at 6:55

You must log in to answer this question.

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