From f39a21cf7b972d038fe41ecbf3ec6aa1ae404dca Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Wed, 30 Nov 2016 09:41:01 -0600 Subject: [PATCH] Provide better feedback on why a name is invalid --- lbrynet/core/Error.py | 10 +++++----- lbrynet/lbrynet_daemon/Daemon.py | 20 ++++++-------------- lbrynet/metadata/Metadata.py | 7 +++++-- tests/unit/core/test_Metadata.py | 13 +++++++------ 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lbrynet/core/Error.py b/lbrynet/core/Error.py index d15b65dc7..91bf48dcc 100644 --- a/lbrynet/core/Error.py +++ b/lbrynet/core/Error.py @@ -32,12 +32,12 @@ class UnknownNameError(Exception): self.name = name -class InvalidNameError(Exception): - def __init__(self, name): +class InvalidName(Exception): + def __init__(self, name, invalid_characters): self.name = name - - def __str__(self): - return repr(self.name) + self.invalid_characters = invalid_characters + Exception.__init__( + self, 'URI contains invalid characters: {}'.format(','.join(invalid_characters))) class UnknownStreamTypeError(Exception): diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 862680b38..8c1d14d40 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -47,7 +47,7 @@ from lbrynet.core.Wallet import LBRYcrdWallet, LBRYumWallet from lbrynet.core.looping_call_manager import LoopingCallManager from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory from lbrynet.core.server.ServerProtocol import ServerProtocolFactory -from lbrynet.core.Error import InsufficientFundsError, InvalidNameError +from lbrynet.core.Error import InsufficientFundsError log = logging.getLogger(__name__) @@ -1620,7 +1620,7 @@ class Daemon(AuthJSONRPCServer): 'metadata': metadata dictionary optional 'fee' Returns: - 'success' : True if claim was succesful , False otherwise + 'success' : True if claim was succesful , False otherwise 'reason' : if not succesful, give reason 'txid' : txid of resulting transaction if succesful 'nout' : nout of the resulting support claim if succesful @@ -1640,16 +1640,8 @@ class Daemon(AuthJSONRPCServer): return d name = p[FileID.NAME] - - log.info("Publish: ") - log.info(p) - - try: - verify_name_characters(name) - except AssertionError: - log.error("Bad name") - return defer.fail(InvalidNameError("Bad name")) - + log.info("Publish: %s", p) + verify_name_characters(name) bid = p['bid'] try: @@ -1663,9 +1655,9 @@ class Daemon(AuthJSONRPCServer): metadata = p['metadata'] file_path = p['file_path'] if not file_path: - return defer.fail(Exception("No file given to publish")) + raise Exception("No file given to publish") if not os.path.isfile(file_path): - return defer.fail(Exception("Specified file for publish doesnt exist: %s" % file_path)) + raise Exception("Specified file for publish doesnt exist: %s" % file_path) self.looping_call_manager.start(Checker.PENDING_CLAIM, 30) diff --git a/lbrynet/metadata/Metadata.py b/lbrynet/metadata/Metadata.py index 2e602b109..82b866d4d 100644 --- a/lbrynet/metadata/Metadata.py +++ b/lbrynet/metadata/Metadata.py @@ -1,5 +1,6 @@ import logging +from lbrynet.core import Error from lbrynet.metadata.StructuredDict import StructuredDict import metadata_schemas @@ -9,10 +10,12 @@ NAME_ALLOWED_CHARSET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987 def verify_name_characters(name): assert len(name) > 0, "Empty uri" - for c in name: - assert c in NAME_ALLOWED_CHARSET, "Invalid character" + invalid_characters = {c for c in name if c not in NAME_ALLOWED_CHARSET} + if invalid_characters: + raise Error.InvalidName(name, invalid_characters) return True + def migrate_001_to_002(metadata): metadata['ver'] = '0.0.2' metadata['nsfw'] = False diff --git a/tests/unit/core/test_Metadata.py b/tests/unit/core/test_Metadata.py index 5c50bb63e..0cf6d8168 100644 --- a/tests/unit/core/test_Metadata.py +++ b/tests/unit/core/test_Metadata.py @@ -1,7 +1,9 @@ -from lbrynet.metadata import Metadata from twisted.trial import unittest from jsonschema import ValidationError +from lbrynet.core import Error +from lbrynet.metadata import Metadata + class MetadataTest(unittest.TestCase): def test_name_error_if_blank(self): @@ -9,14 +11,13 @@ class MetadataTest(unittest.TestCase): Metadata.verify_name_characters("") def test_name_error_if_contains_bad_chrs(self): - with self.assertRaises(AssertionError): + with self.assertRaises(Error.InvalidName): Metadata.verify_name_characters("wu tang") - with self.assertRaises(AssertionError): + with self.assertRaises(Error.InvalidName): Metadata.verify_name_characters("$wutang") - with self.assertRaises(AssertionError): + with self.assertRaises(Error.InvalidName): Metadata.verify_name_characters("#wutang") - def test_validation_error_if_no_metadata(self): metadata = {} with self.assertRaises(ValidationError): @@ -214,4 +215,4 @@ class MetadataTest(unittest.TestCase): 'thumbnail': 'https://svs.gsfc.nasa.gov/vis/a010000/a012000/a012034/Combined.00_08_16_17.Still004.jpg' } m = Metadata.Metadata(metadata, migrate=True) - self.assertEquals('0.0.3', m.version) \ No newline at end of file + self.assertEquals('0.0.3', m.version)