class ValidationError(Exception):
"""Exception raised for validation errors.
Attributes:
reason reason of the error
message explanation of the error
"""
def __init__(self, reason, message="Validation failed"):
self.reason = reason
self.message = message
super().__init__(self.message)
def __str__(self):
# the return value of this function will be the error message
return f'{self.reason} -> {self.message}'
然后您可以将该错误集成到代码中,例如:
def apply(self, transaction, context):
try:
if network_connection is False:
raise ValidationError("Missing Network Connection")
# your code here
if success is False:
raise ValidationError("Unknown reason")
except Exception as e:
print(e)
您可以定义自己的自定义错误,让客户确切地知道发生了什么错误:
然后您可以将该错误集成到代码中,例如:
例如,如果在执行
apply()
函数之前将network_connection
设置为False
,则使用e
打印的消息将是:然后可以将此消息发送到客户端
这不能通过使用验证程序映像来完成。 您必须分叉https://github.com/hyperledger/sawtooth-corerepo并实现您自己的逻辑,以便在任何自定义情况下使验证器回复客户机
如果您希望这样做以避免InvalidTransaction异常的无限循环,那么可以从my forkhttps://github.com/charalarg/sawtooth-core构建并使用验证器,而不是使用锯齿验证器docker映像。它是sawtooth(1.2.6)的最新版本,修复了无限循环错误
我也有同样的问题,我也试图找到一种方法来避免事务执行,因为这个错误,但我最终应用了这个修复。 https://github.com/Remmeauth/sawtooth-core/pull/5
您可以尝试以下方法:
其中,不使用
print()
调用,而是将e
字符串发送到客户端,因为它将包含错误原因相关问题 更多 >
编程相关推荐