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

Exposing exception in StateMachine #601

Open
vghero opened this issue Nov 9, 2018 · 2 comments
Open

Exposing exception in StateMachine #601

vghero opened this issue Nov 9, 2018 · 2 comments

Comments

@vghero
Copy link

vghero commented Nov 9, 2018

I'm using Spring StateMachine 2.0.2 and can find hasStateMachineError() on StateMachine which is nice to check whether an error happened during transition. The problem is, there's no method to actually access what exception occurred. It would be nice if there would be something like getStateMachineError() or something to get access to the exception that was thrown.

@lewisdawson
Copy link

@vghero To gain access to the exception, you need to configure your state machine with a StateMachineListener. The docs recommend extending StateMachineListenerAdapter:

public class MyStateMachineListener extends StateMachineListenerAdapter<State, Event> {
        @Override
        public void stateMachineError(StateMachine<State, Event> stateMachine, Exception exception) {
            // Access your exception here
        }
    }

Then you inject an instantiation of your listener into your state machine configuration:

StateMachineBuilder.builder().configureConfiguration()
                .withConfiguration()
                .listener(new MyStateMachineListener())

@vghero
Copy link
Author

vghero commented Feb 13, 2019

Thanks, that could work. My workaround was, to simply pass a generic error action to the action configuration that simply propagates that exception to the state context's extended state. But your suggestion is cleaner and less boilerplate.

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

2 participants