Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception handling for local saga steps #86

Open
cer opened this issue Sep 7, 2022 · 2 comments
Open

Improve exception handling for local saga steps #86

cer opened this issue Sep 7, 2022 · 2 comments

Comments

@cer
Copy link
Collaborator

cer commented Sep 7, 2022

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)
  • onException() is the equivalent of a try-catch that updates the saga data with the reason for the error. Note: an exception can't be simply stored in the saga data because of the requirement to (de)serialize to/from JSON
  • onExceptionRollback()
    • specifies those exceptions that should cause a rollback.
    • other exceptions are assumed to indicate a transient technical failure and are rethrown by the saga logic - saga creation or reply handling will fail
    • if onExceptionRollback() is not used, then all exceptions cause a rollback (original behavior)
@cer
Copy link
Collaborator Author

cer commented Sep 8, 2022

See LocalExceptionCreateOrderSaga for example usage.

@cer
Copy link
Collaborator Author

cer commented Sep 20, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant