diff options
| author | Cody Logan <cody@lokken.dev> | 2023-10-17 14:00:14 -0700 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-10-17 14:00:14 -0700 |
| commit | 06335ba0176cabd84f5b548995f465ac1c09bc8e (patch) | |
| tree | 1425c62c7371dd1c629a89b3c7a397e8e875268a /src/wikiget | |
| parent | 45a550899e0adf6958764d8a5133da4e21aa7fea (diff) | |
| download | wikiget-06335ba0176cabd84f5b548995f465ac1c09bc8e.tar.gz wikiget-06335ba0176cabd84f5b548995f465ac1c09bc8e.zip | |
Clean up exception handling and error messages
Diffstat (limited to 'src/wikiget')
| -rw-r--r-- | src/wikiget/dl.py | 23 | ||||
| -rw-r--r-- | src/wikiget/parse.py | 5 | ||||
| -rw-r--r-- | src/wikiget/wikiget.py | 11 |
3 files changed, 22 insertions, 17 deletions
diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 50b7460..4521b72 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -24,7 +24,6 @@ from requests import ConnectionError, HTTPError from tqdm import tqdm import wikiget -from wikiget.exceptions import ParseError from wikiget.file import File from wikiget.parse import get_dest from wikiget.validations import verify_hash @@ -42,24 +41,22 @@ def query_api(filename, site_name, args): except ConnectionError as e: # usually this means there is no such site, or there's no network connection, # though it could be a certificate problem - logging.error("Couldn't connect to specified site.") - logging.debug("Full error message:") + logging.error("Could not connect to specified site") logging.debug(e) - sys.exit(1) + raise except HTTPError as e: # most likely a 403 forbidden or 404 not found error for api.php logging.error( - "Couldn't find the specified wiki's api.php. Check the value of --path." + "Could not find the specified wiki's api.php. Check the value of --path." ) - logging.debug("Full error message:") logging.debug(e) - sys.exit(1) + raise except (InvalidResponse, LoginError) as e: # InvalidResponse: site exists, but we couldn't communicate with the API # endpoint for some reason other than an HTTP error. # LoginError: missing or invalid credentials logging.error(e) - sys.exit(1) + raise # get info about the target file try: @@ -70,19 +67,15 @@ def query_api(filename, site_name, args): logging.error( "Access denied. Try providing credentials with --username and --password." ) - logging.debug("Full error message:") for i in e.args: logging.debug(i) - sys.exit(1) + raise return file, site def prep_download(dl, args): - try: - filename, dest, site_name = get_dest(dl, args) - except ParseError: - raise + filename, dest, site_name = get_dest(dl, args) file = File(filename, dest) file.object, file.site = query_api(file.name, site_name, args) return file @@ -158,6 +151,6 @@ def download(f, args): else: # no file information returned - logging.error(f"Target '{filename}' does not appear to be a valid file.") + logging.error(f"Target '{filename}' does not appear to be a valid file") # TODO: log but don't quit while in batch mode sys.exit(1) diff --git a/src/wikiget/parse.py b/src/wikiget/parse.py index 09c0767..f5c221d 100644 --- a/src/wikiget/parse.py +++ b/src/wikiget/parse.py @@ -30,8 +30,9 @@ def get_dest(dl, args): filename = url.path site_name = url.netloc if args.site is not wikiget.DEFAULT_SITE: - # this will work even if the user specifies 'commons.wikimedia.org' - logging.warning("target is a URL, ignoring site specified with --site") + # this will work even if the user specifies 'commons.wikimedia.org' since + # we're comparing objects instead of values (is not vs. !=) + logging.warning("Target is a URL, ignoring site specified with --site") else: filename = dl site_name = args.site diff --git a/src/wikiget/wikiget.py b/src/wikiget/wikiget.py index 68e0233..4446f96 100644 --- a/src/wikiget/wikiget.py +++ b/src/wikiget/wikiget.py @@ -20,6 +20,9 @@ import logging import sys from concurrent.futures import ThreadPoolExecutor +from mwclient import APIError, InvalidResponse, LoginError +from requests import ConnectionError, HTTPError + import wikiget from wikiget.dl import download, prep_download from wikiget.exceptions import ParseError @@ -178,6 +181,10 @@ def batch_download(args): file = prep_download(line, args) except ParseError as e: logging.warning(f"{e} (line {line_num})") + except (ConnectionError, HTTPError, InvalidResponse, LoginError, APIError): + logging.error( + f"Unable to download '{line}' (line {line_num}) due to an error" + ) future = executor.submit(download, file, args) futures.append(future) # wait for downloads to finish @@ -198,6 +205,8 @@ def main(): if args.batch: # batch download mode + # TODO: return non-zero exit code if any errors were encountered, even if some + # downloads completed successfully batch_download(args) else: # single download mode @@ -206,4 +215,6 @@ def main(): except ParseError as e: logging.error(e) sys.exit(1) + except (ConnectionError, HTTPError, InvalidResponse, LoginError, APIError): + sys.exit(1) download(file, args) |
