corda - 如何向节点的用户打印消息(在 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)
}
}
}
解决方案
您应该在流程中引发异常,并带有相应的错误消息。当流程失败时,此错误将自动显示在节点 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.
推荐阅读
- angular - 如何在 HTML 元素属性值计算中使用 ngFor 索引变量?
- azure - 作为一名软件测试人员,我如何测试 Azure 数据湖中的数据?
- python - 使用 ssd_mobilenet_v2_quantized_300x300_coco 的目标检测模型的损失在每 10k-12k 步后增加
- javascript - 当我滚动时,我的导航栏颜色不想用我的 jquery 改变?
- kotlin - 为什么 Kotlin/Java 函数在实际使用中与文档看起来如此不同?
- reactjs - 变异 redux 在开发中保持状态
- excel - Excel函数拆分单元格
- python - 在 python 中解析 POSTed Excel 文件
- visual-studio-code - 多个框架的 VSCode 设置
- javascript - jquery第n个孩子到root