spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.52k stars 598 forks source link

Is there way to throw an Exception that occurred inside the Statemachine to the outside? #1055

Closed KimDoubleB closed 2 years ago

KimDoubleB commented 2 years ago

Hi. I recently got to know spring-statemachine, and I'm learning to use it for a company project.

But there's a problem. There are cases where the logic of guard and action that I wrote generates an exception, and I want to process this exception outside of the state machine. But spring-statemachine handles all these exceptions internally.

Is there an option to throw an internal exception to the outside?

mehmetsalgar commented 2 years ago

State Machines runs with some principle called 'Run to Completion' as defined in this links 1 2, which in short means, when a state machine receives an Event it should start from a State and reach a State.

Now if an Action or Guard cause an exception and interrupts the Event processing and prevents State Machine reaches a State, this would be an undefined behaviour and unacceptable for State Machines.

For this reason, State Machines would not let exceptions escape outside of execution loop.

If there is chance an exception can occur in Guard/Action, it is developers responsibility to deal with it, by writing necessary code for it.

In Spring State Machine there is Event Listener which will trigger it when an Exception occurs, to notify the external client, but that will not stop the execution of the State Machine.

KimDoubleB commented 2 years ago

@mehmetsalgar Thank you for your kind explanation. I understood everything through your explanation. Also, looking at the links you attached, it seems that there are many concepts that I get to know. It's getting more interesting. I'll have to see if I can implement the logic I want using Event Listener. Thank you very much.