From 39b47bc569a339ca3b819cdc0c41d084dab57737 Mon Sep 17 00:00:00 2001 From: kkurokawa Date: Mon, 31 Oct 2016 13:53:45 -0400 Subject: [PATCH] better error detection in _get_stream_info_from_value --- lbrynet/core/Error.py | 5 +++-- lbrynet/core/Wallet.py | 16 ++++++++-------- lbrynet/lbrynet_console/ControlHandlers.py | 2 +- lbrynet/metadata/Metadata.py | 4 +++- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lbrynet/core/Error.py b/lbrynet/core/Error.py index ac4ffcb7a..dcf9bbec6 100644 --- a/lbrynet/core/Error.py +++ b/lbrynet/core/Error.py @@ -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): diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py index 883e80fc5..3171f2f6b 100644 --- a/lbrynet/core/Wallet.py +++ b/lbrynet/core/Wallet.py @@ -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) diff --git a/lbrynet/lbrynet_console/ControlHandlers.py b/lbrynet/lbrynet_console/ControlHandlers.py index 25054000a..6b5e075f8 100644 --- a/lbrynet/lbrynet_console/ControlHandlers.py +++ b/lbrynet/lbrynet_console/ControlHandlers.py @@ -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: diff --git a/lbrynet/metadata/Metadata.py b/lbrynet/metadata/Metadata.py index 39522022f..a078d1f01 100644 --- a/lbrynet/metadata/Metadata.py +++ b/lbrynet/metadata/Metadata.py @@ -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) \ No newline at end of file + StructuredDict.__init__(self, metadata, starting_version, migrate, target_version)