From 156f117b18ebd997a7e08454ab21455ca6491e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 27 Aug 2016 13:49:19 +0200 Subject: [PATCH] Add Bot initiation with exception handling Bot initiation needs to catch errors by Bot to enforce at least a basic logging. And also to be sure Init was successfull before starting bot. Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=82 FS#82] --- red.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/red.py b/red.py index dd14625..7a26f24 100644 --- a/red.py +++ b/red.py @@ -48,7 +48,7 @@ def main(*args): local_args = pywikibot.handle_args(args) # Get the jogobot-task_slug (basename of current file without ending) - task_slug = os.path.basename(__file__)[:-len(".py")] # noqa (temp) + task_slug = os.path.basename(__file__)[:-len(".py")] # Before run, we need to check wether we are currently active or not try: @@ -86,7 +86,7 @@ def main(*args): subtask = None # kwargs are passed to selected bot as **kwargs - kwargs = dict() # noqa (temp) + kwargs = dict() # Parse command line arguments for arg in local_args: @@ -108,7 +108,7 @@ def main(*args): subtask = "discparser" # Import related bot - from bots.reddiscparser import DiscussionParserBot as Bot # noqa (temp) + from bots.reddiscparser import DiscussionParserBot as Bot # else: @@ -116,6 +116,25 @@ def main(*args): "\03{{red}} Given subtask \"{subtask} \"" + "is not existing!" ).format( subtask=subtask ), "ERROR" ) + # Bot gets prepared genFactory as first param and possible kwargs dict + # It has to threw an exception if something does not work properly + try: + # Init bot with genFactory and **kwargs + bot = Bot( genFactory, **kwargs ) # noqa (temp) + + except: + # Catch Errors while initiation + jogobot.output( ( + "\03{{red}} Error while trying to init " + + "subtask \"{task_slug}-{subtask} \"!" ). + format( task_slug=task_slug, subtask=subtask ), "ERROR" ) + raise + else: + # Init successfull + jogobot.output( ( + "{task_slug}-{subtask} init successfull" ). + format(task_slug=task_slug, subtask=subtask) ) + if( __name__ == "__main__" ): main()