From 6a5a704c75aec983aa2006a22e69d7a9d388b576 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sun, 20 Nov 2016 16:24:34 +0000 Subject: [PATCH] Update docs - Add section about error actions for states. - Relates to #270 --- docs/src/reference/asciidoc/sm.adoc | 18 ++++++-- .../docs/DocsConfigurationSampleTests.java | 43 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/docs/src/reference/asciidoc/sm.adoc b/docs/src/reference/asciidoc/sm.adoc index d6811f4b..93d13cf6 100644 --- a/docs/src/reference/asciidoc/sm.adoc +++ b/docs/src/reference/asciidoc/sm.adoc @@ -221,9 +221,8 @@ wrapped within a `Runnable` which may get cancelled via action, you need to be able to catch `InterruptedException` which is raised if task is cancelled. -[[statemachine-config-actions-errorhandling]] +[[statemachine-config-transition-actions-errorhandling]] ==== Transition Action Error Handling - User can always catch exceptions manually but with actions defined for transitions it is possible to define error action which is called if exception is reased. Exception is then available from a `StateContext` @@ -241,6 +240,19 @@ Similar logic can be done manually for every action if needed. include::samples/DocsConfigurationSampleTests.java[tags=snippetED] ---- +[[statemachine-config-state-actions-errorhandling]] +==== State Action Error Handling +Similar logic for error handling what is available for transition +actions is also available for actions defined for state behaviour and +its entry and exit. + +For these `StateConfigurer` has methods `stateEntry`, `stateDo` and +`stateExit` to define `error` action together with an actual `action`. + +[source,java,indent=0] +---- +include::samples/DocsConfigurationSampleTests.java[tags=snippetEE] +---- === Configuring Pseudo States @@ -1608,7 +1620,7 @@ include::samples/DocsConfigurationSampleTests.java[tags=snippet4] [TIP] ==== Actions defined for transitions also have their own error handling -logic <>. +logic <>. ==== [[sm-persist]] diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java index cd26a0c8..e9dc2091 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/docs/DocsConfigurationSampleTests.java @@ -353,6 +353,49 @@ public class DocsConfigurationSampleTests extends AbstractStateMachineTests { } } +// tag::snippetEE[] + @Configuration + @EnableStateMachine + public class Config55 + extends EnumStateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineStateConfigurer states) + throws Exception { + states + .withStates() + .initial(States.S1) + .stateEntry(States.S2, action(), errorAction()) + .stateDo(States.S2, action(), errorAction()) + .stateExit(States.S2, action(), errorAction()) + .state(States.S3); + } + + @Bean + public Action action() { + return new Action() { + + @Override + public void execute(StateContext context) { + throw new RuntimeException("MyError"); + } + }; + } + + @Bean + public Action errorAction() { + return new Action() { + + @Override + public void execute(StateContext context) { + // RuntimeException("MyError") added to context + Exception exception = context.getException(); + exception.getMessage(); + } + }; + } + } +// end::snippetEE[] // tag::snippetFA[] @Configuration