如何打印消息(在shell或http响应上)给节点的用户?

时间:2019-01-29 09:08:17

标签: corda

我写了一个无法成功完成的流程。因此,我想让用户知道原因,并向他显示一些说明。 如果节点用户使用shell或HTTP客户端,那么都能够做到这一点真是棒极了。

您能否提供指向正确论文或指南的链接?

UPD

package com.template

import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.*
import net.corda.core.utilities.ProgressTracker
import net.corda.core.contracts.Command
import net.corda.core.identity.Party
import net.corda.core.transactions.TransactionBuilder
import net.corda.core.utilities.unwrap

// *********
// * Flows *
// *********
@InitiatingFlow
@StartableByRPC
class SandboxSellerFlow(val iouValue: Int,
              val otherParty: Party) : FlowLogic<Unit>() {

    /** The progress tracker provides checkpoints indicating the progress of the flow to observers. */
    override val progressTracker = ProgressTracker()

    /** The flow logic is encapsulated within the call() method. */
    @Suspendable
    override fun call() {
        // We retrieve the notary identity from the network map.
        val notary = serviceHub.networkMapCache.notaryIdentities[0]
        val flowSession = initiateFlow(otherParty)

        // We create the transaction components.
        val outputState = IOUState(iouValue, ourIdentity)

        val packet = flowSession.sendAndReceive<Boolean>(iouValue)
        val res = packet.unwrap{data->data}

        if(res)
            throw FlowException("I've been rejected")

        val command = Command(TemplateContract.Commands.Action(), ourIdentity.owningKey)

        // We create a transaction builder and add the components.
        val txBuilder = TransactionBuilder(notary = notary)
                .addOutputState(outputState, TemplateContract.ID)
                .addCommand(command)

        // We sign the transaction.
        val signedTx = serviceHub.signInitialTransaction(txBuilder)

        // We finalise the transaction.
        subFlow(FinalityFlow(signedTx))
    }
}

@StartableByRPC
@InitiatedBy(SandboxSellerFlow::class)
class SandboxBuyerFlow(internal val sellerSession: FlowSession) : FlowLogic<Unit>() {
    override val progressTracker = ProgressTracker()

    @Suspendable
    override fun call(){
        // Wait for a trade request to come in from the other party.
        val any = sellerSession.receive<Any>().unwrap{data->data}
        if(any is Int) {
            if (any > 10)
                sellerSession.send(false)
            else
                sellerSession.send(true)
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您应在流中引发异常并显示相应的错误消息。流失败时,此错误将自动显示在节点外壳中。如果使用HTTP客户端,则需要捕获异常并向用户显示错误消息。

例如,假设我有流程:

@InitiatingFlow
@StartableByRPC
class Initiator : FlowLogic<Unit>() {
    override val progressTracker = ProgressTracker()

    @Suspendable
    override fun call() {
        throw FlowException("This flow always throws an exception.")
    }
}

如果我从外壳程序运行此流程,则会收到以下异常:

Tue Jan 29 12:46:21 GMT 2019>>> start Initiator


   Done
☠   This flow always throws an exception.