From f16a8fe911ab1f49463a837b38d1350299d6932b Mon Sep 17 00:00:00 2001 From: skyuecx0630 <48788794+skyuecx0630@users.noreply.github.com> Date: Wed, 7 Aug 2024 16:06:40 +0900 Subject: [PATCH] Handle botocore.exceptions.ClientError --- services/s3.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services/s3.py b/services/s3.py index 92442cb..e167f63 100644 --- a/services/s3.py +++ b/services/s3.py @@ -1,5 +1,6 @@ from models import RuleCheckResult import boto3 +import botocore.exceptions client = boto3.client("s3") @@ -36,8 +37,8 @@ def s3_bucket_default_lock_enabled(): try: response = client.get_object_lock_configuration(Bucket=bucket["Name"]) compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") - except Exception as e: - if e.__class__.__name__ == "ObjectLockConfigurationNotFoundError": + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == "ObjectLockConfigurationNotFoundError": non_compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") else: raise e @@ -197,8 +198,8 @@ def s3_lifecycle_policy_check(): try: configuration = client.get_bucket_lifecycle_configuration(Bucket=bucket["Name"]) compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") - except Exception as e: - if e.__class__.__name__ == "NoSuchLifecycleConfiguration": + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == "NoSuchLifecycleConfiguration": non_compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") else: raise e