eventuate-tram / eventuate-tram-sagas

Sagas for microservices
Other
1k stars 228 forks source link

Improve exception handling for local saga steps #86

Open cer opened 2 years ago

cer commented 2 years ago

Current behavior

If a local step throws an exception the saga is rolled back.

Application code, e.g. onSagaRolledBack() does not have access to the exception and so cannot doesn't know the reason.

Also, the rollback occurs for any exception, not just 'business-related' exceptions. Consequently, a transient technical failure will result in a rollback

Proposal

            .step()
                  .invokeLocal(steps::approveOrder)
                    .onException(InvalidOrderException.class, LocalExceptionCreateOrderSagaData::saveInvalidOrder)
                    .onExceptionRollback(InvalidOrderException.class)
cer commented 2 years ago

See LocalExceptionCreateOrderSaga for example usage.

cer commented 2 years ago

Proposal: 'Global' exception handlers

Define Spring @Beans that implement the following interface:

interface LocalStepExceptionHandler<E extends Exception, SagaData> extends Ordered   {
     void handleException(E e, SagaData sagaData);
}

If a local step throws an exception, the first (sorted by Ordered) applicable - to exception and saga data - is invoked to update the SagaData prior to starting compensation.