raise a specific error when spaces are detected, pass along the docstring to the user

This commit is contained in:
Chris Wolfe 2016-06-02 14:12:54 -07:00
parent d4d3320277
commit 5be436b81d
2 changed files with 12 additions and 3 deletions

View File

@ -4,7 +4,8 @@ start = time.time()
import os, sys, textwrap
from twisted.internet.defer import maybeDeferred
from twisted.internet.task import react
from ..errors import TransferError, WrongPasswordError, WelcomeError, Timeout
from ..errors import (TransferError, WrongPasswordError, WelcomeError, Timeout,
KeyFormatError)
from ..timing import DebugTiming
from .cli_args import parser
top_import_finish = time.time()
@ -49,7 +50,8 @@ def run(reactor, argv, cwd, stdout, stderr, executable=None):
d.addBoth(_maybe_dump_timing)
def _explain_error(f):
# these errors don't print a traceback, just an explanation
f.trap(TransferError, WrongPasswordError, WelcomeError, Timeout)
f.trap(TransferError, WrongPasswordError, WelcomeError, Timeout,
KeyFormatError)
if f.check(WrongPasswordError):
msg = textwrap.fill("ERROR: " + textwrap.dedent(f.value.__doc__))
print(msg, file=stderr)

View File

@ -15,7 +15,7 @@ from . import __version__
from . import codes
#from .errors import ServerError, Timeout
from .errors import (WrongPasswordError, UsageError, WelcomeError,
WormholeClosedError)
WormholeClosedError, KeyFormatError)
from .timing import DebugTiming
from .util import (to_bytes, bytes_to_hexstr, hexstr_to_bytes,
dict_to_bytes, bytes_to_dict)
@ -476,6 +476,13 @@ class _Wormhole:
def _event_learned_code(self, code):
self._timing.add("code established")
# bail out early if the password contains spaces...
# this should raise a useful error
if ' ' in code:
raise KeyFormatError(
"code (%s) contains spaces. Words must be separated by dashes"
% code
)
self._code = code
mo = re.search(r'^(\d+)-', code)
if not mo: