ENT-1962: throw a clearer exception when starting RPC flow while SMM is stopped … (#912)

* ENT-1962: throw a clearer exception when starting RPC flow while SMM is stopped.

* ENT-1962: update exception constructor
This commit is contained in:
bpaunescu 2018-06-04 10:14:53 +01:00 committed by GitHub
parent 76644e0d00
commit 71d8586e61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 2 deletions

View File

@ -39,6 +39,17 @@ class LifeCycle<S : Enum<S>>(initial: S) {
}
fun requireState(requiredState: S) = requireState(requiredState) {}
fun <A> requireState(
requiredState: S,
throwable: Throwable,
block: () -> A
): A {
return lock.readLock().withLock {
if (requiredState != state) { throw throwable }
block()
}
}
/** Assert something about the current state atomically. */
fun <A> requireState(
errorMessage: (S) -> String,

View File

@ -50,6 +50,7 @@ import net.corda.core.node.services.vault.Sort
import net.corda.core.serialization.serialize
import net.corda.core.transactions.SignedTransaction
import net.corda.core.utilities.getOrThrow
import net.corda.node.internal.exceptions.StateMachineStoppedException
import net.corda.node.services.api.FlowStarter
import net.corda.node.services.api.ServiceHubInternal
import net.corda.node.services.messaging.context
@ -185,7 +186,11 @@ internal class CordaRPCOpsImpl(
if (isFlowsDrainingModeEnabled()) {
throw RejectedCommandException("Node is draining before shutdown. Cannot start new flows through RPC.")
}
return flowStarter.invokeFlowAsync(logicType, context(), *args).getOrThrow()
try {
return flowStarter.invokeFlowAsync(logicType, context(), *args).getOrThrow()
} catch (e: StateMachineStoppedException) {
throw RejectedCommandException("Node is shutting down. Cannot start new flows through RPC.")
}
}
override fun attachmentExists(id: SecureHash): Boolean {

View File

@ -0,0 +1,7 @@
package net.corda.node.internal.exceptions
import net.corda.core.CordaRuntimeException
class StateMachineStoppedException(message: String, cause: Throwable?) : CordaRuntimeException(message, cause) {
constructor(msg: String) : this(msg, null)
}

View File

@ -34,6 +34,7 @@ import net.corda.core.utilities.Try
import net.corda.core.utilities.contextLogger
import net.corda.core.utilities.debug
import net.corda.node.internal.InitiatedFlowFactory
import net.corda.node.internal.exceptions.StateMachineStoppedException
import net.corda.node.services.api.CheckpointStorage
import net.corda.node.services.api.ServiceHubInternal
import net.corda.node.services.config.shouldCheckCheckpoints
@ -196,7 +197,7 @@ class MultiThreadedStateMachineManager(
ourIdentity: Party?,
deduplicationHandler: DeduplicationHandler?
): CordaFuture<FlowStateMachine<A>> {
return lifeCycle.requireState(State.STARTED) {
return lifeCycle.requireState(State.STARTED, StateMachineStoppedException("Flow cannot be started. State machine is stopped.")) {
startFlowInternal(
invocationContext = context,
flowLogic = flowLogic,