imgtool: update a few errors to use click's
Click has better UI for exceptions, so instead of throwing a backtrace, allow it to print a nicer error message. Signed-off-by: Fabio Utzig <utzig@apache.org>
This commit is contained in:
parent
9a492d5e87
commit
1f50892096
|
@ -156,13 +156,16 @@ class Image():
|
|||
def load(self, path):
|
||||
"""Load an image from a given file"""
|
||||
ext = os.path.splitext(path)[1][1:].lower()
|
||||
if ext == INTEL_HEX_EXT:
|
||||
ih = IntelHex(path)
|
||||
self.payload = ih.tobinarray()
|
||||
self.base_addr = ih.minaddr()
|
||||
else:
|
||||
with open(path, 'rb') as f:
|
||||
self.payload = f.read()
|
||||
try:
|
||||
if ext == INTEL_HEX_EXT:
|
||||
ih = IntelHex(path)
|
||||
self.payload = ih.tobinarray()
|
||||
self.base_addr = ih.minaddr()
|
||||
else:
|
||||
with open(path, 'rb') as f:
|
||||
self.payload = f.read()
|
||||
except FileNotFoundError:
|
||||
raise click.UsageError("Input file not found")
|
||||
|
||||
# Add the image header if needed.
|
||||
if self.pad_header and self.header_size > 0:
|
||||
|
@ -180,8 +183,8 @@ class Image():
|
|||
if ext == INTEL_HEX_EXT:
|
||||
# input was in binary format, but HEX needs to know the base addr
|
||||
if self.base_addr is None and hex_addr is None:
|
||||
raise Exception("No address exists in input file neither was "
|
||||
"it provided by user")
|
||||
raise click.UsageError("No address exists in input file "
|
||||
"neither was it provided by user")
|
||||
h = IntelHex()
|
||||
if hex_addr is not None:
|
||||
self.base_addr = hex_addr
|
||||
|
@ -381,7 +384,8 @@ class Image():
|
|||
return MAX_ALIGN * 2 + magic_size
|
||||
else:
|
||||
if write_size not in set([1, 2, 4, 8]):
|
||||
raise Exception("Invalid alignment: {}".format(write_size))
|
||||
raise click.BadParameter("Invalid alignment: {}".format(
|
||||
write_size))
|
||||
m = DEFAULT_MAX_SECTORS if max_sectors is None else max_sectors
|
||||
trailer = m * 3 * write_size # status area
|
||||
if enckey is not None:
|
||||
|
|
|
@ -22,6 +22,7 @@ import imgtool.keys as keys
|
|||
import sys
|
||||
from imgtool import image, imgtool_version
|
||||
from imgtool.version import decode_version
|
||||
from .keys import RSAUsageError, ECDSAUsageError, Ed25519UsageError
|
||||
|
||||
|
||||
def gen_rsa2048(keyfile, passwd):
|
||||
|
@ -116,7 +117,10 @@ def getpriv(key, minimal):
|
|||
key = load_key(key)
|
||||
if key is None:
|
||||
print("Invalid passphrase")
|
||||
key.emit_private(minimal)
|
||||
try:
|
||||
key.emit_private(minimal)
|
||||
except (RSAUsageError, ECDSAUsageError, Ed25519UsageError) as e:
|
||||
raise click.UsageError(e)
|
||||
|
||||
|
||||
@click.argument('imgfile')
|
||||
|
@ -254,7 +258,8 @@ def sign(key, align, version, header_size, pad_header, slot_size, pad,
|
|||
or (isinstance(key, keys.RSA) and
|
||||
not isinstance(enckey, keys.RSAPublic))):
|
||||
# FIXME
|
||||
raise Exception("Signing and encryption must use the same type of key")
|
||||
raise click.UsageError("Signing and encryption must use the same "
|
||||
"type of key")
|
||||
img.create(key, enckey, dependencies)
|
||||
img.save(outfile, hex_addr)
|
||||
|
||||
|
|
Loading…
Reference in New Issue