Skip to content

Commit e14f5e8

Browse files
committed
Remove class Failed
1 parent 491d8bb commit e14f5e8

4 files changed

Lines changed: 6 additions & 77 deletions

File tree

lib/src/main/kotlin/io/nexure/fsm/StateMachine.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ interface StateMachine<S : Any, E : Any, N : Any> {
2727
* with [signal] as input. Returns a [Transition] indicating if the transition was permitted and
2828
* successful or not.
2929
*
30-
* Any [Exception] that is thrown will be caught and a part of the Transition return value.
31-
* Note that only instances of [Exception] will be caught, and instances of [Throwable] which do
32-
* not inherit from [Exception] - like [Error] - will not be caught.
33-
*
3430
* It is recommended that the return value is checked for the desired outcome, if it is critical
35-
* that an event for example is accepted and not rejected, or that any thrown exceptions are
36-
* handled or reported.
31+
* that an event for example is accepted and not rejected.
3732
*/
3833
fun onEvent(current: S, event: E, signal: N): Transition<S>
3934

lib/src/main/kotlin/io/nexure/fsm/StateMachineImpl.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ internal class StateMachineImpl<S : Any, E : Any, N : Any>(
4444

4545
override fun onEvent(current: S, event: E, signal: N): Transition<S> {
4646
val next: S = nextState(current, event) ?: return Rejected
47-
return try {
48-
executeTransition(current, next, event, signal)
49-
Executed(next)
50-
} catch (e: Exception) {
51-
Failed(e)
52-
}
47+
executeTransition(current, next, event, signal)
48+
return Executed(next)
5349
}
5450

5551
private fun nextState(current: S, event: E): S? {

lib/src/main/kotlin/io/nexure/fsm/Transition.kt

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,22 @@ package io.nexure.fsm
55
* rejected by the state machine, or if there was an exception while processing the event.
66
*/
77
sealed class Transition<out S : Any> {
8-
fun transitioned(): Boolean {
9-
return when (this) {
10-
is Executed -> true
11-
is Failed, Rejected -> false
12-
}
13-
}
8+
fun transitioned(): Boolean = this is Executed<S>
149

1510
/**
1611
* Returns
1712
* - The new state if the transition was permitted and successful ([Executed])
1813
* - `null` if the transitioned was rejected ([Rejected])
19-
* - Throws an exception if such occurred ([Failed])
2014
*/
21-
fun stateOrThrow(): S? {
15+
fun stateOrNull(): S? {
2216
return when (this) {
2317
is Executed -> this.state
2418
Rejected -> null
25-
is Failed -> throw this.exception
2619
}
2720
}
2821

2922
/**
30-
* Invoke this lambda if a transition was exceuted and successful
23+
* Invoke this lambda if a transition was executed and successful
3124
*/
3225
inline fun onExecution(handle: (state: S) -> Unit): Transition<S> {
3326
if (this is Executed) {
@@ -45,17 +38,6 @@ sealed class Transition<out S : Any> {
4538
}
4639
return this
4740
}
48-
49-
/**
50-
* Invoke this lambda if an exception was thrown by either an interceptor or during the
51-
* execution of the action associated with the event/transition
52-
*/
53-
inline fun onFailure(handle: (exception: Exception) -> Unit): Transition<S> {
54-
if (this is Failed) {
55-
handle(this.exception)
56-
}
57-
return this
58-
}
5941
}
6042

6143
/**
@@ -73,12 +55,3 @@ data class Executed<S : Any>(val state: S) : Transition<S>() {
7355
object Rejected : Transition<Nothing>() {
7456
override fun toString(): String = "Rejected"
7557
}
76-
77-
/**
78-
* The event could not be processed due to an exception while executing the action associated
79-
* with this state transition, or while executing any interceptors, if such are present.
80-
* Some part of the action may have completed while others may not have.
81-
*/
82-
data class Failed(val exception: Exception) : Transition<Nothing>() {
83-
override fun toString(): String = "Failed($exception)"
84-
}

lib/src/test/kotlin/io/nexure/fsm/StateMachineTest.kt

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,6 @@ class StateMachineTest {
155155
assertEquals(32, value)
156156
}
157157

158-
@Test
159-
fun `test exception in interceptor is caught and returned as failure`() {
160-
val exception = IllegalArgumentException("foo")
161-
val fsm = StateMachine.builder<State, Event, Int>()
162-
.initial(State.S1)
163-
.connect(State.S1, State.S2, Event.E2)
164-
.intercept { _, _, _, _ -> throw exception }
165-
.build()
166-
167-
assertEquals(Failed(exception), fsm.onEvent(State.S1, Event.E2, 0))
168-
}
169-
170-
@Test
171-
fun `test exception in post interceptor is caught and returned as failure`() {
172-
val exception = IllegalArgumentException("foo")
173-
val fsm = StateMachine.builder<State, Event, Int>()
174-
.initial(State.S1)
175-
.connect(State.S1, State.S2, Event.E2)
176-
.postIntercept { _, _, _, _ -> throw exception }
177-
.build()
178-
179-
assertEquals(Failed(exception), fsm.onEvent(State.S1, Event.E2, 0))
180-
}
181-
182158
@Test
183159
fun `test transition with Action`() {
184160
val semaphore = Semaphore(10)
@@ -237,17 +213,6 @@ class StateMachineTest {
237213
assertEquals(Rejected, fsm.onEvent(State.S1, Event.E3, false))
238214
}
239215

240-
@Test
241-
fun `test exception in action is caught`() {
242-
val exception = IllegalArgumentException("foo")
243-
val fsm = StateMachine.builder<State, Event, Boolean>()
244-
.initial(State.S1)
245-
.connect(State.S1, State.S2, Event.E1) { throw exception }
246-
.build()
247-
248-
assertEquals(Failed(exception), fsm.onEvent(State.S1, Event.E1, false))
249-
}
250-
251216
@Test(expected = StackOverflowError::class)
252217
fun `test throwable in action is not caught`() {
253218
val exception = StackOverflowError("foo")

0 commit comments

Comments
 (0)