Golang chaincode与消息一起发送错误代码

问题描述 投票:0回答:1

我还想使用shim.Error函数向客户端应用程序发送错误消息以外的错误代码,但它只接受msg参数,该怎么做?

hyperledger-fabric chaincode
1个回答
1
投票

shim.Error返回响应结构。

func Error(msg string) pb.Response {
    return pb.Response{
        Status:  ERROR,
        Message: msg,
    }
}

状态设置为ERROR const,定义为500.但您可以使用任何> = 400的错误代码。

因此,您可以自己构建响应并设置状态代码,而不是使用Error函数。

return pb.Response{
        Status:  404,
        Message: "Invoke method you wanted to trigger does not exist",
    } 

或者您可以编写自己的错误函数,该函数也接受状态代码,并确保它在错误范围内。

最后一个选项是使用响应的Payload属性,并添加错误的详细信息,包括有效负载中的状态代码。

© www.soinside.com 2019 - 2024. All rights reserved.