Skip to content

Fix action error in anonymous transition #351

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

Merged
merged 1 commit into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.statemachine.StateContext.Stage;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachineException;
import org.springframework.statemachine.access.StateMachineAccess;
import org.springframework.statemachine.access.StateMachineAccessor;
import org.springframework.statemachine.access.StateMachineFunction;
Expand Down Expand Up @@ -310,8 +311,10 @@ public void transit(Transition<S, E> t, StateContext<S, E> ctx, Message<E> messa
try {
t.executeTransitionActions(ctx);
} catch (Exception e) {
// aborting, executor should stop possible loop checking possible transitions
// causing infinite execution
log.warn("Aborting as transition " + t + " caused error " + e);
return;
throw new StateMachineException("Aborting as transition " + t + " caused error ", e);
}
notifyTransition(buildStateContext(Stage.TRANSITION, message, t, getRelayStateMachine()));
if (t.getTarget().getPseudoState() != null && t.getTarget().getPseudoState().getKind() == PseudoStateKind.JOIN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ private boolean handleTriggerTrans(List<Transition<S, E>> trans, Message<E> queu
log.warn("Aborting as transition " + t + " caused error " + e);
}
if (transit) {
stateMachineExecutorTransit.transit(t, stateContext, queuedMessage);
// if executor transit is raising exception, stop here
try {
stateMachineExecutorTransit.transit(t, stateContext, queuedMessage);
} catch (Exception e) {
interceptors.postTransition(stateContext);
return false;
}
interceptors.postTransition(stateContext);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ public void testActionExceptionNotCausingStateChange() {
assertThat(machine.getState().getIds(), contains(TestStates.S1));
}

@Test
public void testActionExceptionInAnonymousTransition() {
context.register(Config2.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
machine.start();
// error in transition should not cause transition and should
// not propagate error into a caller.
machine.sendEvent(TestEvents.E1);
assertThat(machine.getState().getIds(), contains(TestStates.S2));
}

@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
Expand Down Expand Up @@ -84,6 +99,42 @@ public TestCountAction testAction1() {

}

@Configuration
@EnableStateMachine
static class Config2 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {

@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.S1)
.state(TestStates.S2)
.end(TestStates.SF);
}

@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
transitions
.withExternal()
.source(TestStates.S1)
.target(TestStates.S2)
.event(TestEvents.E1)
.and()
.withExternal()
.source(TestStates.S2)
.target(TestStates.SF)
.action(context -> {
throw new NullPointerException("Something was wrong");
});
}

@Bean
public TestCountAction testAction1() {
return new TestCountAction();
}

}

private static class TestCountAction implements Action<TestStates, TestEvents> {

@Override
Expand Down