Logging Salesforce errors with platform events
The following pattern is fairly common in other technologies:
try {
doSomethingRisky();
} catch (Exception e) {
logException(e);
throw e;
}
This won’t work in salesforce. By rethrowing the exception (assuming it’s
not caught further up the call stack), the database transaction will
be rolled back - that includes whatever insert
or update
ws triggered in
logException(e)
. So how do we ensure that the logging occurs in a separate
database transaction? The answer is a platform event!