Better error formatting.

This commit is contained in:
dachshund 2013-09-18 03:12:39 -04:00
parent f9a1ac9a4c
commit 8d3bffa10c
2 changed files with 26 additions and 10 deletions

View file

@ -80,7 +80,14 @@ class UnsupportedAlgorithmError(Error):
class BadHashError(Error):
"""Indicate an error while checking the value a hash object."""
pass
def __init__(self, expected_hash, observed_hash):
self.expected_hash = expected_hash
self.observed_hash = observed_hash
def __str__(self):
return 'Observed hash ('+str(self.observed_hash)+\
') != expected hash ('+str(self.expected_hash)+')'
@ -120,7 +127,12 @@ class ForbiddenTargetError(RepositoryError):
class ExpiredMetadataError(Error):
"""Indicate that a TUF Metadata file has expired."""
pass
def __init__(self, expiry_time):
self.expiry_time = expiry_time # UTC
def __str__(self):
return 'Metadata expired on '+str(self.expiry_time)+'.'
@ -154,8 +166,12 @@ class CryptoError(Error):
class BadSignatureError(CryptoError):
"""Indicate that some metadata file had a bad signature."""
pass
def __init__(self, metadata_role_name):
self.metadata_role_name = metadata_role_name
def __str__(self):
return str(self.metadata_role_name)+' metadata bad signature!'

View file

@ -627,8 +627,7 @@ def __check_hashes(self, file_object, trusted_hashes):
digest_object.update(file_object.read())
computed_hash = digest_object.hexdigest()
if trusted_hash != computed_hash:
raise tuf.BadHashError('Hashes do not match! Expected '+
trusted_hash+' got '+computed_hash)
raise tuf.BadHashError(trusted_hash, computed_hash)
else:
logger.info('The file\'s '+algorithm+' hash is correct: '+trusted_hash)
@ -835,7 +834,7 @@ def __verify_uncompressed_metadata_file(self, metadata_file_object,
# Verify the signature on the downloaded metadata object.
valid = tuf.sig.verify(metadata_signable, metadata_role)
if not valid:
raise tuf.BadSignatureError()
raise tuf.BadSignatureError(metadata_role)
@ -1742,10 +1741,11 @@ def _ensure_not_expired(self, metadata_role):
# an exception. 'expires' is in YYYY-MM-DD HH:MM:SS format, so
# convert it to seconds since the epoch, which is the time format
# returned by time.time() (i.e., current time), before comparing.
if tuf.formats.parse_time(expires) < time.time():
message = 'Metadata '+repr(rolepath)+' expired on '+repr(expires)+'.'
logger.error(message)
raise tuf.ExpiredMetadataError(message)
current_time = time.time()
expiry_time = tuf.formats.parse_time(expires)
if expiry_time < current_time:
logger.error('Metadata '+repr(rolepath)+' expired on '+repr(expires)+'.')
raise tuf.ExpiredMetadataError(expires)