diff --git a/trezorlib/client.py b/trezorlib/client.py index 77aac35d2..9440952e9 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -152,7 +152,7 @@ class BaseClient(object): if isinstance(resp, proto.Failure): if resp.code == proto.FailureType.ActionCancelled: raise exceptions.Cancelled - raise exceptions.TrezorException(resp.code, resp.message) + raise exceptions.TrezorFailure(resp) return resp diff --git a/trezorlib/exceptions.py b/trezorlib/exceptions.py index b028f0809..b8ae8ba39 100644 --- a/trezorlib/exceptions.py +++ b/trezorlib/exceptions.py @@ -2,6 +2,26 @@ class TrezorException(Exception): pass +class TrezorFailure(TrezorException): + def __init__(self, failure): + self.failure = failure + # TODO: this is backwards compatibility with tests. it should be changed + super().__init__(self.failure.code, self.failure.message) + + def __str__(self): + from .messages import FailureType + + types = { + getattr(FailureType, name): name + for name in dir(FailureType) + if not name.startswith("_") + } + if self.failure.message is not None: + return "{}: {}".format(types[self.failure.code], self.failure.message) + else: + return types[self.failure.code] + + class PinException(TrezorException): pass