start on cleanups: error catching in CLI dispatch

This commit is contained in:
Brian Warner 2016-04-25 17:03:23 -07:00
parent a4c1ba9e4e
commit b96b052c96

View File

@ -2,7 +2,7 @@ from __future__ import print_function
import os, sys import os, sys
from twisted.internet.defer import maybeDeferred from twisted.internet.defer import maybeDeferred
from twisted.internet.task import react from twisted.internet.task import react
from ..errors import TransferError from ..errors import TransferError, WrongPasswordError, Timeout
from ..timing import DebugTiming from ..timing import DebugTiming
from .cli_args import parser from .cli_args import parser
@ -35,6 +35,7 @@ def run(reactor, argv, cwd, stdout, stderr, executable=None):
args.timing = timing = DebugTiming() args.timing = timing = DebugTiming()
timing.add_event("command dispatch") timing.add_event("command dispatch")
# fires with None, or raises an error
d = maybeDeferred(dispatch, args) d = maybeDeferred(dispatch, args)
def _maybe_dump_timing(res): def _maybe_dump_timing(res):
timing.add_event("exit") timing.add_event("exit")
@ -43,13 +44,12 @@ def run(reactor, argv, cwd, stdout, stderr, executable=None):
return res return res
d.addBoth(_maybe_dump_timing) d.addBoth(_maybe_dump_timing)
def _explain_error(f): def _explain_error(f):
f.trap(TransferError) # these three errors don't print a traceback, just an explanation
f.trap(TransferError, WrongPasswordError, Timeout)
print("ERROR:", f.value, file=stderr) print("ERROR:", f.value, file=stderr)
raise SystemExit(1) raise SystemExit(1)
d.addErrback(_explain_error) d.addErrback(_explain_error)
def _rc(rc): d.addCallback(lambda _: 0)
raise SystemExit(rc)
d.addCallback(_rc)
return d return d
def entry(): def entry():