我还想使用shim.Error
函数向客户端应用程序发送错误消息以外的错误代码,但它只接受msg
参数,该怎么做?
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属性,并添加错误的详细信息,包括有效负载中的状态代码。