Golang 链代码随消息一起发送错误代码

Golang chaincode send an error code along with the message

我也想用shim.Error函数向客户端应用程序发送错误信息以外的错误代码,但它只接受一个msg参数,怎么办?

shim.Error returns 响应结构。

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

Status 设置为 ERROR const,定义为 500。但是您可以使用任何您想要的错误代码,即 >= 400。

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

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

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

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