From 460d2db18396939c13f7fbb9ca1627fbf4cb02a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 27 Aug 2016 14:00:29 +0200 Subject: [PATCH] Add Bot run with exception handling Errors, especially caused by missing run-method, need to be catched to provide information in Logfile. And also to get information wether bot run was successfull Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=82 FS#82] Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=82 FS#82] --- red.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/red.py b/red.py index 7a26f24..3d398f4 100644 --- a/red.py +++ b/red.py @@ -34,7 +34,7 @@ from pywikibot import pagegenerators import jogobot -def main(*args): +def main(*args): # noqa (temp) """ Process command line arguments and invoke bot. @@ -120,7 +120,7 @@ def main(*args): # 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) + bot = Bot( genFactory, **kwargs ) except: # Catch Errors while initiation @@ -132,7 +132,44 @@ def main(*args): else: # Init successfull jogobot.output( ( - "{task_slug}-{subtask} init successfull" ). + "Subtask \"{task_slug}-{subtask}\" was" + + "initiated successfully" ). + format(task_slug=task_slug, subtask=subtask) ) + + # Fire up Bot + # Bot must have implemented a run()-method + # It has to threw an exception if something does not work properly + try: + # Call run method on Bot + bot.run() + + # Special event on AttributeError to catch missing run()-method + except AttributeError: + (type, value, traceback) = sys.exc_info() + + # Catch missing run()-method + if "has no attribute 'run'" in value: + jogobot.output( ( + "\03{{red}} Error while trying to run " + + "subtask \"{task_slug}-{subtask} \": +" + "Run-method is missing! "). + format( task_slug=task_slug, subtask=subtask ), "ERROR" ) + + # Pass through other AttributeError + else: + raise + + except: + jogobot.output( ( + "\03{{red}} Error while trying to run " + + "subtask \"{task_slug}-{subtask} \"!" ). + format( task_slug=task_slug, subtask=subtask ), "ERROR" ) + raise + + else: + # Run successfull + jogobot.output( ( + "Subtask \"{task_slug}-{subtask}\" was finished successfully"). format(task_slug=task_slug, subtask=subtask) )