1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 12:08:59 +00:00

fix exceptions - writeout errors instead of throwing tracebacks

This commit is contained in:
Dominik Kozaczko 2016-09-28 00:01:32 +02:00 committed by Pavol Rusnak
parent f379752794
commit 300bf5801d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -6,9 +6,12 @@ import argparse
import json
import base64
import tempfile
from trezorlib import types_pb2 as types
from io import BytesIO
from trezorlib.client import TrezorClient, TrezorClientDebug
import sys
from trezorlib.client import TrezorClient, TrezorClientDebug, CallException
ether_units = {
"wei": 1,
@ -70,7 +73,7 @@ def get_transport(transport_string, path, **kwargs):
if path == '' or path in d:
return HidTransport(d, **kwargs)
raise Exception("Device not found")
raise CallException(types.Failure_Other, "Device not found")
if transport_string == 'udp':
from trezorlib.transport_udp import UdpTransport
@ -90,7 +93,7 @@ def get_transport(transport_string, path, **kwargs):
if path == '' or d['path'] == binascii.hexlify(path):
return BridgeTransport(d, **kwargs)
raise Exception("Device not found")
raise CallException(types.Failure_Other, "Device not found")
raise NotImplementedError("Unknown transport")
@ -124,7 +127,7 @@ class Commands(object):
if ' ' in value:
value, unit = value.split(' ', 1)
if unit.lower() not in ether_units:
raise Exception("Unrecognized ether unit %r", unit)
raise CallException(types.Failure_Other, "Unrecognized ether unit %r" % unit)
value = int(value) * ether_units[unit.lower()]
else:
value = int(value)
@ -196,7 +199,7 @@ class Commands(object):
from PIL import Image
im = Image.open(args.filename)
if im.size != (128, 64):
raise Exception('Wrong size of the image')
raise CallException(types.Failure_Other, 'Wrong size of the image')
im = im.convert('1')
pix = im.load()
img = ''
@ -223,7 +226,7 @@ class Commands(object):
def load_device(self, args):
if not args.mnemonic and not args.xprv:
raise Exception("Please provide mnemonic or xprv")
raise CallException(types.Failure_Other, "Please provide mnemonic or xprv")
if args.mnemonic:
mnemonic = ' '.join(args.mnemonic)
@ -313,7 +316,7 @@ class Commands(object):
if fp[:8] == b'54525a52':
fp = binascii.unhexlify(fp)
if fp[:4] != b'TRZR':
raise Exception("TREZOR firmware header expected")
raise CallException(types.Failure_FirmwareError, "TREZOR firmware header expected")
print("Please confirm action on device...")
@ -534,6 +537,8 @@ def qt_pin_func(input_text, message=None):
def main():
args = parse_args(Commands)
try:
if args.cmd == 'list':
devices = list_usb()
if args.json:
@ -560,6 +565,11 @@ def main():
print(json.dumps(res, sort_keys=True, indent=4))
else:
print(res)
except CallException as e:
status, message = e.args
sys.stderr.write('failure: {message}\n'.format(message=message))
exit(status)
if __name__ == '__main__':
main()