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): class InvalidStreamInfoError(Exception):
def __init__(self, name): def __init__(self, name, stream_info):
self.name = name self.name = name
self.stream_info = stream_info
def __str__(self): def __str__(self):
return repr(self.name) return '{} has claim with invalid stream info: {}'.format(self.name,self.stream_info)
class MisbehavingPeerError(Exception): class MisbehavingPeerError(Exception):

View file

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

View file

@ -900,7 +900,7 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash):
def _resolve_name(self, name): def _resolve_name(self, name):
def get_name_from_info(stream_info): def get_name_from_info(stream_info):
if 'stream_hash' not in 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.resolved_name = stream_info.get('name', None)
self.description = stream_info.get('description', None) self.description = stream_info.get('description', None)
try: try:

View file

@ -33,6 +33,8 @@ class Metadata(StructuredDict):
] ]
def __init__(self, metadata, migrate=True, target_version=None): 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') 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)