From afa48c2d2de918af0b52024709a198a9a20dd642 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Fri, 8 May 2020 23:35:25 +0200 Subject: [PATCH] Swallow and print errors instead of crashing --- PFERD/errors.py | 28 ++++++++++++++++++++++++++++ PFERD/pferd.py | 3 +++ 2 files changed, 31 insertions(+) create mode 100644 PFERD/errors.py diff --git a/PFERD/errors.py b/PFERD/errors.py new file mode 100644 index 0000000..c12d23a --- /dev/null +++ b/PFERD/errors.py @@ -0,0 +1,28 @@ +""" +An error logging decorator. +""" + +import logging +from typing import Any, Callable + +from .logging import FatalException, PrettyLogger + +LOGGER = logging.getLogger(__name__) +PRETTY = PrettyLogger(LOGGER) + + +def swallow_and_print_errors(function: Callable) -> Callable: + """ + Decorates a function, swallows all errors, logs them and returns none if one occurred. + """ + def inner(*args: Any, **kwargs: Any) -> Any: + # pylint: disable=broad-except + try: + return function(*args, **kwargs) + except FatalException as error: + PRETTY.error(str(error)) + return None + except Exception as error: + Console().print_exception() + return None + return inner diff --git a/PFERD/pferd.py b/PFERD/pferd.py index 62e9dd2..74e54c5 100644 --- a/PFERD/pferd.py +++ b/PFERD/pferd.py @@ -9,6 +9,7 @@ from typing import List, Optional, Union from .cookie_jar import CookieJar from .diva import (DivaDownloader, DivaDownloadStrategy, DivaPlaylistCrawler, diva_download_new) +from .errors import swallow_and_print_errors from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter, IliasDownloader, IliasDownloadStrategy, KitShibbolethAuthenticator, download_modified_or_new) @@ -89,6 +90,7 @@ class Pferd(Location): return organizer + @swallow_and_print_errors def ilias_kit( self, target: PathLike, @@ -141,6 +143,7 @@ class Pferd(Location): clean=clean, ) + @swallow_and_print_errors def diva_kit( self, target: Union[PathLike, Organizer],