From 8571f042166d7e9fd8872e329bb6127f42d13c73 Mon Sep 17 00:00:00 2001 From: Vincent A Date: Mon, 19 Feb 2018 23:16:33 +0100 Subject: [PATCH] weboob.tools.application: let bcall_errors_handler return exit code Currently, weboob commands still return 0 even when an exception is raised. Let bcall_errors_handler decide the return code instead of always 0. A good return code is hard because of several things: * there might be multiple exceptions in a BCallErrors, so values returned by bcall_error_handler (single exception) cannot easily be combined * the base implemententation of bcall_errors_handler cannot be polluted with a lot of exception subclasses So, any non-MoreResultsAvailable will cause a non-zero return code. Should fix https://git.weboob.org/weboob/devel/issues/74. --- weboob/tools/application/base.py | 11 ++++++++++- weboob/tools/application/console.py | 10 ++++++++-- weboob/tools/application/repl.py | 6 +++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index 55692962a1..14fc739cfa 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -344,6 +344,8 @@ def bcall_errors_handler(self, errors, debugmsg='Use --debug option to print bac :param ignore: Exceptions to ignore :type ignore: tuple[:class:`Exception`] """ + err = 0 + ask_debug_mode = False for backend, error, backtrace in errors.errors: if isinstance(error, ignore): @@ -351,9 +353,14 @@ def bcall_errors_handler(self, errors, debugmsg='Use --debug option to print bac elif self.bcall_error_handler(backend, error, backtrace): ask_debug_mode = True + if not isinstance(error, MoreResultsAvailable): + err = 1 + if ask_debug_mode: print(debugmsg, file=self.stderr) + return err + def _shell_completion_items(self): items = set() for ol in [self._parser.option_list] + [og.option_list for og in self._parser.option_groups]: @@ -506,9 +513,11 @@ def run(cls, args=None): sys.exit(1) except CallErrors as e: try: - app.bcall_errors_handler(e) + ret = app.bcall_errors_handler(e) except KeyboardInterrupt: pass + else: + sys.exit(ret) sys.exit(1) except ResultsConditionError as e: print('%s' % e, file=cls.stderr) diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index ebfce55fec..c54b2da1ca 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -647,15 +647,21 @@ def bcall_errors_handler(self, errors, debugmsg='Use --debug option to print bac """ ask_debug_mode = False more_results = set() + err = 0 + for backend, error, backtrace in errors.errors: if isinstance(error, MoreResultsAvailable): more_results.add(backend.name) elif isinstance(error, ignore): continue - elif self.bcall_error_handler(backend, error, backtrace): - ask_debug_mode = True + else: + err = 1 + if self.bcall_error_handler(backend, error, backtrace): + ask_debug_mode = True if ask_debug_mode: print(debugmsg, file=self.stderr) elif len(more_results) > 0: print('Hint: There are more results available for %s (use option -n or count command)' % (', '.join(more_results)), file=self.stderr) + + return err diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index 8f2ad023e5..88ca3883c9 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -453,7 +453,7 @@ def onecmd(self, line): try: return super(ReplApplication, self).onecmd(line) except CallErrors as e: - self.bcall_errors_handler(e) + return self.bcall_errors_handler(e) except BackendNotGiven as e: print('Error: %s' % str(e), file=self.stderr) return os.EX_DATAERR @@ -550,9 +550,9 @@ def bcall_error_handler(self, backend, error, backtrace): def bcall_errors_handler(self, errors, ignore=()): if self.interactive: - ConsoleApplication.bcall_errors_handler(self, errors, 'Use "logging debug" option to print backtraces.', ignore) + return super(ReplApplication, self).bcall_errors_handler(errors, 'Use "logging debug" option to print backtraces.', ignore) else: - ConsoleApplication.bcall_errors_handler(self, errors, ignore=ignore) + return super(ReplApplication, self).bcall_errors_handler(errors, ignore=ignore) # -- options related methods ------------- def _handle_options(self): -- GitLab