forked from LBRYCommunity/lbry-sdk
Provide better feedback on why a name is invalid
This commit is contained in:
parent
8cdda7d845
commit
f39a21cf7b
4 changed files with 23 additions and 27 deletions
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
self.assertEquals('0.0.3', m.version)
|
||||
|
|
Loading…
Add table
Reference in a new issue