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

@ -344,8 +344,8 @@ class Wallet(object):
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']
@ -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)