From 87104e7e6a0a244c7cad60f85df231f775071323 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Thu, 24 Nov 2022 10:48:17 +1000 Subject: [PATCH] Catch exceptions in cloner --- bdfr/cloner.py | 9 +++++++-- .../test_clone_integration.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bdfr/cloner.py b/bdfr/cloner.py index 979f50f..47e03f8 100644 --- a/bdfr/cloner.py +++ b/bdfr/cloner.py @@ -3,6 +3,8 @@ import logging +import prawcore + from bdfr.archiver import Archiver from bdfr.configuration import Configuration from bdfr.downloader import RedditDownloader @@ -17,5 +19,8 @@ class RedditCloner(RedditDownloader, Archiver): def download(self): for generator in self.reddit_lists: for submission in generator: - self._download_submission(submission) - self.write_entry(submission) + try: + self._download_submission(submission) + self.write_entry(submission) + except prawcore.PrawcoreException as e: + logger.error(f'Submission {submission.id} failed to be cloned due to a PRAW exception: {e}') diff --git a/tests/integration_tests/test_clone_integration.py b/tests/integration_tests/test_clone_integration.py index f9bf91a..288793b 100644 --- a/tests/integration_tests/test_clone_integration.py +++ b/tests/integration_tests/test_clone_integration.py @@ -36,6 +36,8 @@ def create_basic_args_for_cloner_runner(test_args: list[str], tmp_path: Path): ['-s', 'TrollXChromosomes/', '-L', 1], ['-l', 'eiajjw'], ['-l', 'xl0lhi'], + ['-l', 'ijy4ch'], # user deleted post + ['-l', 'kw4wjm'], # post from banned subreddit )) def test_cli_scrape_general(test_args: list[str], tmp_path: Path): runner = CliRunner() @@ -44,3 +46,19 @@ def test_cli_scrape_general(test_args: list[str], tmp_path: Path): assert result.exit_code == 0 assert 'Downloaded submission' in result.output assert 'Record for entry item' in result.output + + +@pytest.mark.online +@pytest.mark.reddit +@pytest.mark.skipif(not does_test_config_exist, reason='A test config file is required for integration tests') +@pytest.mark.parametrize('test_args', ( + ['-l', 'ijy4ch'], # user deleted post + ['-l', 'kw4wjm'], # post from banned subreddit +)) +def test_cli_scrape_soft_fail(test_args: list[str], tmp_path: Path): + runner = CliRunner() + test_args = create_basic_args_for_cloner_runner(test_args, tmp_path) + result = runner.invoke(cli, test_args) + assert result.exit_code == 0 + assert 'Downloaded submission' not in result.output + assert 'Record for entry item' not in result.output