aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Schofield <git@ajschof.me>2025-03-03 00:42:05 +0000
committerAlex Schofield <git@ajschof.me>2025-03-03 00:42:05 +0000
commite939b481a53be8e9075efec594b9d9efc6f712b4 (patch)
tree29037b58adef3dc1fdab57a4d91d3d009ecf2ff3
parentcd0c6ff8328597f3f23f1642a0796baba9b12b34 (diff)
downloadgdpr-obfuscator-e939b481a53be8e9075efec594b9d9efc6f712b4.tar.gz
gdpr-obfuscator-e939b481a53be8e9075efec594b9d9efc6f712b4.zip
improve error handling in read_s3()
-rw-r--r--gdpr_obfuscator/read.py21
1 files changed, 17 insertions, 4 deletions
diff --git a/gdpr_obfuscator/read.py b/gdpr_obfuscator/read.py
index 1d80718..48e94c6 100644
--- a/gdpr_obfuscator/read.py
+++ b/gdpr_obfuscator/read.py
@@ -59,10 +59,23 @@ class FileHandler:
client = boto3.client("s3")
- response = client.get_object(Bucket=bucket, Key=key)
- content = response["Body"].read().decode("utf-8")
- read_csv_content = self.read_string(content)
- return read_csv_content
+ try:
+ response = client.get_object(Bucket=bucket, Key=key)
+ except client.exceptions.NoSuchKey:
+ raise ValueError(f"File not found in S3 bucket: {bucket}/{key}")
+ except client.exceptions.NoSuchBucket:
+ raise ValueError(f"Bucket not found in S3: {bucket}")
+ except client.exceptions.ClientError as e:
+ raise ValueError(f"Error accessing S3: {e}")
+
+ try:
+ content = response["Body"].read().decode("utf-8")
+ except UnicodeDecodeError:
+ raise ValueError("File is not UTF-8 encoded or malformed")
+ except Exception as e:
+ raise ValueError(f"Error reading file from S3: {e}")
+
+ return self.read_string(content)
@staticmethod
def read_string(content: str) -> List[Dict[str, str]]:
git.ajschof.me — hosted by ajschofield — powered by cgit