首页 > 解决方案 > 如何向节点的用户打印消息(在 shell 或 http 响应上)?

问题描述

我写了一个流程,但无法成功完成。所以我想让用户知道原因并向他展示带有一些描述的消息。如果节点用户使用 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)
        }
    }

}

标签: corda

解决方案


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

例如,假设我有流程:

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

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

如果我从 shell 运行这个流程,我会得到以下异常:

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


   Done
☠   This flow always throws an exception.

推荐阅读