From e57ed0cd5ba92220e09fe80ab04cce0b3cccfaeb Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Fri, 14 Apr 2017 20:01:52 +0200 Subject: [bug] improve exception catching in execution loop --- src/leap/bitmask/cli/bitmask_cli.py | 30 ++++++++++++++++++++++-------- src/leap/bitmask/cli/vpn.py | 5 ++++- src/leap/bitmask/core/dispatcher.py | 1 + 3 files changed, 27 insertions(+), 9 deletions(-) (limited to 'src/leap/bitmask') diff --git a/src/leap/bitmask/cli/bitmask_cli.py b/src/leap/bitmask/cli/bitmask_cli.py index ef76cb47..f138e486 100755 --- a/src/leap/bitmask/cli/bitmask_cli.py +++ b/src/leap/bitmask/cli/bitmask_cli.py @@ -21,6 +21,7 @@ Bitmask Command Line interface: zmq client. import json import sys import signal +import traceback from colorama import Fore from twisted.internet import reactor, defer @@ -142,11 +143,15 @@ def execute(): cli = BitmaskCLI(cfg) cli.data = ['core', 'version'] args = None if '--noverbose' in sys.argv else ['--verbose'] - yield cli._send( - timeout=0.1, printer=_null_printer, - errb=lambda: cli.start(args)) - if 'start' in sys.argv or 'restart' in sys.argv: - command.default_dict_printer({'start': 'ok'}) + + try: + yield cli._send( + timeout=0.1, printer=_null_printer, + errb=lambda: cli.start(args)) + except Exception, e: + print (Fore.RED + "ERROR: " + Fore.RESET + + "%s" % e.strerror) + defer.returnValue('') cli.data = [] cli.print_json = print_json @@ -154,11 +159,19 @@ def execute(): if print_json: args.remove('--json') - yield cli.execute(args) try: + yield cli.execute(args) + if 'start' in sys.argv or 'restart' in sys.argv: + command.default_dict_printer({'start': 'ok'}) + except Exception, e: + if hasattr(e, 'strerror'): + print (Fore.RED + "ERROR: " + Fore.RESET + + "%s" % e.strerror) + else: + if not hasattr(e, 'expected'): + print traceback.format_exc() + finally: yield reactor.stop() - except: - pass def _null_printer(*args): @@ -166,6 +179,7 @@ def _null_printer(*args): def main(): + def signal_handler(signal, frame): if reactor.running: reactor.stop() diff --git a/src/leap/bitmask/cli/vpn.py b/src/leap/bitmask/cli/vpn.py index 60c57aac..0c5eddec 100644 --- a/src/leap/bitmask/cli/vpn.py +++ b/src/leap/bitmask/cli/vpn.py @@ -62,7 +62,10 @@ SUBCOMMANDS: try: _, provider = uid.split('@') except ValueError: - raise ValueError("A provider is needed to start the VPN") + error = ValueError() + error.strerror = "A provider is needed to start the VPN" + error.expected = True + raise error self.data += ['start', provider] diff --git a/src/leap/bitmask/core/dispatcher.py b/src/leap/bitmask/core/dispatcher.py index c026c490..9d486009 100644 --- a/src/leap/bitmask/core/dispatcher.py +++ b/src/leap/bitmask/core/dispatcher.py @@ -541,6 +541,7 @@ def _format_error(failure): # If a exception declares the 'expected' attribute as True, # we will not print a full traceback + expected = getattr(failure.value, 'expected', False) if not expected: try: -- cgit v1.2.3