spring-statemachine icon indicating copy to clipboard operation
spring-statemachine copied to clipboard

Spring State Machine Error Handling not working

Open CodeYogiCo opened this issue 7 years ago • 3 comments

I am getting an exception

org.springframework.security.access.AccessDeniedException: Access is denied
	at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:84)
	at org.springframework.statemachine.security.StateMachineSecurityInterceptor.decide(StateMachineSecurityInterceptor.java:190)
	at org.springframework.statemachine.security.StateMachineSecurityInterceptor.decide(StateMachineSecurityInterceptor.java:157)
	at org.springframework.statemachine.security.StateMachineSecurityInterceptor.preTransition(StateMachineSecurityInterceptor.java:109)
	at org.springframework.statemachine.support.StateMachineInterceptorList.preTransition(StateMachineInterceptorList.java:128)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor.handleTriggerTrans(DefaultStateMachineExecutor.java:266)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor.handleTriggerTrans(DefaultStateMachineExecutor.java:211)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor.processTriggerQueue(DefaultStateMachineExecutor.java:449)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor.access$200(DefaultStateMachineExecutor.java:65)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor$1.run(DefaultStateMachineExecutor.java:323)
	at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor.scheduleEventQueueProcessing(DefaultStateMachineExecutor.java:352)
	at org.springframework.statemachine.support.DefaultStateMachineExecutor.execute(DefaultStateMachineExecutor.java:163)
	at org.springframework.statemachine.support.AbstractStateMachine.sendEventInternal(AbstractStateMachine.java:603)
	at org.springframework.statemachine.support.AbstractStateMachine.sendEvent(AbstractStateMachine.java:218)
	at org.springframework.statemachine.support.AbstractStateMachine.sendEvent(AbstractStateMachine.java:230)

This behavior is as expected by application based on spring security. However i want to catch this exception throw it back to my api user. For the same I have written a custom listener.

public class ErrorStateMachineListener
    extends StateMachineListenerAdapter<AgreementSubStatus, AgreementEvents> {

  @Override
  public void stateMachineError(
      StateMachine<AgreementSubStatus, AgreementEvents> stateMachine, Exception exception) {
    //System.out.println("here i am -------------");
    //throw new IllegalStateTransitionException("Illegal state transition.");
  }

 @Override
  public void stateEntered(State<AgreementSubStatus, AgreementEvents> state){
   //System.out.println("here i am 2-------------"+state.getId().name());
 }
}

And registered this listener in


    builder
                .configureConfiguration()
                .withConfiguration()
                .listener(new ErrorStateMachineListener())
                .autoStartup(true)
                .and()
                .withSecurity()
                .enabled(true);

However the control never goes to this method --public void stateMachineError

CodeYogiCo avatar Apr 05 '18 06:04 CodeYogiCo

Any feedback on this issue? We are having the same problem as described here. Ideally we'd like to register a global error handler that deals with exceptions rather than littering the transition configuration with , errorAction)

esanchezros avatar Jun 03 '18 19:06 esanchezros

Hi, I saw similar question\bug report mentioned three times. Is anyone handling it?

https://github.com/spring-projects/spring-statemachine/issues/553 https://github.com/spring-projects/spring-statemachine/issues/340

tovisofer avatar Mar 10 '19 17:03 tovisofer

Running into this in 2020 and it's a blocker to adoption.

banderous avatar Dec 08 '20 09:12 banderous