better error detection in _get_stream_info_from_value

This commit is contained in:
kkurokawa 2016-10-31 13:53:45 -04:00
parent 9535a24706
commit 39b47bc569
4 changed files with 15 additions and 12 deletions

View file

@ -53,11 +53,12 @@ class InvalidStreamDescriptorError(Exception):
class InvalidStreamInfoError(Exception):
def __init__(self, name):
def __init__(self, name, stream_info):
self.name = name
self.stream_info = stream_info
def __str__(self):
return repr(self.name)
return '{} has claim with invalid stream info: {}'.format(self.name,self.stream_info)
class MisbehavingPeerError(Exception):

View file

@ -331,22 +331,22 @@ class Wallet(object):
def _check_result_fields(r):
for k in ['value', 'txid', 'n', 'height', 'amount']:
assert k in r, "getvalueforname response missing field %s" % k
def _log_success(claim_id):
log.info("lbry://%s complies with %s, claimid: %s", name, metadata.version, claim_id)
return defer.succeed(None)
if 'error' in result:
log.warning("Got an error looking up a name: %s", result['error'])
return Failure(UnknownNameError(name))
_check_result_fields(result)
try:
metadata = Metadata(json.loads(result['value']))
except ValidationError:
return Failure(InvalidStreamInfoError(name))
metadata = Metadata(json.loads(result['value']))
except (TypeError,ValueError,ValidationError):
return Failure(InvalidStreamInfoError(name,result['value']))
txid = result['txid']
sd_hash = metadata['sources']['lbry_sd_hash']
d = self._save_name_metadata(name, txid, sd_hash)
@ -427,7 +427,7 @@ class Wallet(object):
meta_ver = metadata.version
sd_hash = metadata['sources']['lbry_sd_hash']
d = self._save_name_metadata(name, txid, sd_hash)
except ValidationError:
except (TypeError,ValueError,ValidationError):
metadata = claim['value']
meta_ver = "Non-compliant"
d = defer.succeed(None)

View file

@ -900,7 +900,7 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash):
def _resolve_name(self, name):
def get_name_from_info(stream_info):
if 'stream_hash' not in stream_info:
raise InvalidStreamInfoError(name)
raise InvalidStreamInfoError(name,stream_info)
self.resolved_name = stream_info.get('name', None)
self.description = stream_info.get('description', None)
try:

View file

@ -33,6 +33,8 @@ class Metadata(StructuredDict):
]
def __init__(self, metadata, migrate=True, target_version=None):
if not isinstance(metadata,dict):
raise TypeError("metadata is not a dictionary")
starting_version = metadata.get('ver', '0.0.1')
StructuredDict.__init__(self, metadata, starting_version, migrate, target_version)
StructuredDict.__init__(self, metadata, starting_version, migrate, target_version)