diff options
| author | Alex Schofield <git@ajschof.me> | 2024-08-22 12:33:00 +0100 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2024-08-22 12:33:00 +0100 |
| commit | 053e75bca8ef34a655bb4afda5f479f112dfb002 (patch) | |
| tree | 9135f84f76ed10d12e430063d559f685f977af5f | |
| parent | dc7dfe29ce977f3038fb3affd617683e8f163dc8 (diff) | |
| download | de-project-bentley-053e75bca8ef34a655bb4afda5f479f112dfb002.tar.gz de-project-bentley-053e75bca8ef34a655bb4afda5f479f112dfb002.zip | |
fix: improve error handling for list_existing_s3_files and tests
| -rw-r--r-- | src/extract_lambda.py | 16 | ||||
| -rw-r--r-- | tests/test_extract_lambda.py | 10 |
2 files changed, 18 insertions, 8 deletions
diff --git a/src/extract_lambda.py b/src/extract_lambda.py index 874098b..b20c99d 100644 --- a/src/extract_lambda.py +++ b/src/extract_lambda.py @@ -118,15 +118,16 @@ def list_existing_s3_files(bucket_name=None, client=None): results of listing the contents of the s3 bucket, then returns the populated dictionary """ - if client is None: - client = boto3.client("s3") - if bucket_name is None: - bucket_name = extract_bucket(client) logging.info("Listing existing S3 files") existing_files = {} try: + if client is None: + client = boto3.client("s3") + if bucket_name is None: + bucket_name = extract_bucket(client) + response = client.list_objects_v2(Bucket=bucket_name) if "Contents" in response: @@ -142,8 +143,11 @@ def list_existing_s3_files(bucket_name=None, client=None): logger.error("The bucket is empty") return None - except ClientError as e: - logger.error(f"Error listing S3 objects: {e}") + except ValueError as ve: + logger.error(f"Error listing S3 objects: {ve}") + raise + except ClientError as ce: + logger.error(f"Error listing S3 objects: {ce}") return existing_files diff --git a/tests/test_extract_lambda.py b/tests/test_extract_lambda.py index bba433c..8fa0e88 100644 --- a/tests/test_extract_lambda.py +++ b/tests/test_extract_lambda.py @@ -195,8 +195,14 @@ class TestListExistingS3Files: logger.info("Testing now.") caplog.set_level(logging.ERROR) - with pytest.raises(ValueError, match="No extract_bucket found"): - list_existing_s3_files(client=s3_client) + # Mock the extract_bucket function to raise a ValueError! + with patch( + "src.extract_lambda.extract_bucket", + side_effect=ValueError("No extract_bucket found"), + ): + with pytest.raises(ValueError, match="No extract_bucket found"): + list_existing_s3_files(client=s3_client) + assert "Error listing S3 objects" in caplog.text def test_error_if_bucket_is_empty(self, s3_client, caplog, s3_mock_bucket): |
