From b295ee1be1f6ed0ee847c274637c5638ed88563f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= Date: Sun, 3 Jan 2021 01:26:52 +0100 Subject: Better error message on failed update runner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The feed update runner can fail for various reasons, most notably connection errors when receiving the feed URL. Currently, this will result in a Python exception being propagated to the main runner loop and will result in a not-so-useful traceback without any indication on what the error runner actually said when it failed. To fix this, catch the CalledProcessError directly in the updater and turn it into a meaningful error message with the command output instead of a Python traceback. This gives both a more meaningful error message, and a cleaner output without Python tracebacks. Signed-off-by: Toke Høiland-Jørgensen --- nextcloud_news_updater/api/cli.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/nextcloud_news_updater/api/cli.py b/nextcloud_news_updater/api/cli.py index cc68dfd..4b2b428 100644 --- a/nextcloud_news_updater/api/cli.py +++ b/nextcloud_news_updater/api/cli.py @@ -1,4 +1,4 @@ -from subprocess import check_output +from subprocess import check_output, CalledProcessError, STDOUT from typing import List, Any from nextcloud_news_updater.api.api import Api, Feed @@ -9,7 +9,7 @@ from nextcloud_news_updater.config import Config class Cli: def run(self, commands: List[str]) -> bytes: - return check_output(commands) + return check_output(commands, stderr=STDOUT) class CliApi(Api): @@ -73,12 +73,20 @@ class CliUpdateThread(UpdateThread): self.cli = cli self.api = api + def run_command(self, command: List[str]) -> None: + self.logger.info('Running update command: %s' % ' '.join(command)) + try: + self.cli.run(command) + except CalledProcessError as e: + self.logger.error("Command '%s' returned %d with output: '%s'" % + (' '. join(command), + e.returncode, + e.output.decode().strip())) + def update_feed(self, feed: Feed) -> None: command = self.api.update_feed_command + [str(feed.feed_id), feed.user_id] - self.logger.info('Running update command: %s' % ' '.join(command)) - self.cli.run(command) - + self.run_command(command) class CliUpdateThreadV15(CliUpdateThread): """Cli Updater for Nextcloud News v15+""" @@ -86,9 +94,7 @@ class CliUpdateThreadV15(CliUpdateThread): def update_feed(self, feed: Feed) -> None: command = self.api.update_feed_command + [feed.user_id, str(feed.feed_id)] - self.logger.info('Running update command: %s' % ' '.join(command)) - self.cli.run(command) - + self.run_command(command) class CliUpdater(Updater): def __init__(self, config: Config, logger: Logger, api: CliApi, -- cgit v1.2.3