From 8b23c2f38f4ba23e3716d755231114a247e713e8 Mon Sep 17 00:00:00 2001 From: Steven Meyer <108885656+meyertst-aws@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:44:00 -0400 Subject: [PATCH] adding exception handling --- python/example_code/ssm/document.py | 10 ++++++++- python/example_code/ssm/hello.py | 8 ++++++- python/example_code/ssm/maintenance_window.py | 16 +++++++++++++- python/example_code/ssm/test/test_document.py | 16 +++----------- .../ssm/test/test_maintenance_window.py | 22 +++++++++++++++++-- 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/python/example_code/ssm/document.py b/python/example_code/ssm/document.py index b498d6f992c..7b18c09c562 100644 --- a/python/example_code/ssm/document.py +++ b/python/example_code/ssm/document.py @@ -43,7 +43,7 @@ def create(self, content, name): ) self.name = name except self.ssm_client.exceptions.DocumentAlreadyExists: - logger.warning("Document %s already exists. ", name) + print(f"Document {name} already exists.") self.name = name except ClientError as err: logger.error( @@ -128,6 +128,14 @@ def describe(self): try: response = self.ssm_client.describe_document(Name=self.name) return response["Document"]["Status"] + except self.ssm_client.exceptions.InvalidDocument as err: + logger.error( + "Document %s is not valid. %s: %s", + self.name, + err.response["Error"]["Code"], + err.response["Error"]["Message"], + ) + raise except ClientError as err: logger.error( "Couldn't get %s. Here's why: %s: %s", diff --git a/python/example_code/ssm/hello.py b/python/example_code/ssm/hello.py index 5f177f55370..0cd59f236c8 100644 --- a/python/example_code/ssm/hello.py +++ b/python/example_code/ssm/hello.py @@ -3,6 +3,7 @@ # snippet-start:[python.example_code.ssm.Hello] import boto3 +from botocore.exceptions import ClientError def hello_systems_manager(ssm_client): @@ -25,5 +26,10 @@ def hello_systems_manager(ssm_client): if __name__ == "__main__": - hello_systems_manager(boto3.client("ssm")) + try: + hello_systems_manager(boto3.client("ssm")) + except ClientError as err: + print("Hello systems manager had an error.") + print(err.response["Error"]["Code"]) + print(err.response["Error"]["Message"]) # snippet-end:[python.example_code.ssm.Hello] diff --git a/python/example_code/ssm/maintenance_window.py b/python/example_code/ssm/maintenance_window.py index 75edca644e8..a4c641b974d 100644 --- a/python/example_code/ssm/maintenance_window.py +++ b/python/example_code/ssm/maintenance_window.py @@ -4,7 +4,7 @@ import logging import boto3 -from botocore.exceptions import ClientError +from botocore.exceptions import ClientError, ParamValidationError logger = logging.getLogger(__name__) @@ -52,6 +52,13 @@ def create(self, name, schedule, duration, cutoff, allow_unassociated_targets): self.window_id = response["WindowId"] self.name = name logger.info("Created maintenance window %s.", self.window_id) + except ParamValidationError as error: + logger.error( + "Parameter validation error when trying to create maintenance window %s. Here's why: %s", + self.window_id, + error, + ) + raise except ClientError as err: logger.error( "Couldn't create maintenance window %s. Here's why: %s: %s", @@ -113,6 +120,13 @@ def update( ) self.name = name logger.info("Updated maintenance window %s.", self.window_id) + except ParamValidationError as error: + logger.error( + "Parameter validation error when trying to update maintenance window %s. Here's why: %s", + self.window_id, + error, + ) + raise except ClientError as err: logger.error( "Couldn't update maintenance window %s. Here's why: %s: %s", diff --git a/python/example_code/ssm/test/test_document.py b/python/example_code/ssm/test/test_document.py index 4bdc401551c..00a389dd84b 100644 --- a/python/example_code/ssm/test/test_document.py +++ b/python/example_code/ssm/test/test_document.py @@ -10,19 +10,10 @@ from botocore.exceptions import ClientError from document import DocumentWrapper -from logging.handlers import QueueHandler -import logging -import queue - -log_queue = queue.LifoQueue() - -queue_handler = QueueHandler(log_queue) -document_logger = logging.getLogger("document") -document_logger.addHandler(queue_handler) @pytest.mark.parametrize("error_code", [None, "TestException", "DocumentAlreadyExists"]) -def test_create(make_stubber, error_code): +def test_create(make_stubber, capsys, error_code): ssm_client = boto3.client("ssm") ssm_stubber = make_stubber(ssm_client) document_wrapper = DocumentWrapper(ssm_client) @@ -63,9 +54,8 @@ def test_create(make_stubber, error_code): name, ) assert document_wrapper.name == name - assert ( - log_queue.qsize() > 0 and "already exists" in log_queue.get().getMessage() - ) + capt = capsys.readouterr() + assert "already exists" in capt.out else: with pytest.raises(ClientError) as exc_info: document_wrapper.create( diff --git a/python/example_code/ssm/test/test_maintenance_window.py b/python/example_code/ssm/test/test_maintenance_window.py index 8bf970434e9..9bb8dfd4cff 100644 --- a/python/example_code/ssm/test/test_maintenance_window.py +++ b/python/example_code/ssm/test/test_maintenance_window.py @@ -7,12 +7,21 @@ import boto3 import pytest -from botocore.exceptions import ClientError +from botocore.exceptions import ClientError, ParamValidationError from maintenance_window import MaintenanceWindowWrapper +from logging.handlers import QueueHandler +import logging +import queue +log_queue = queue.LifoQueue() -@pytest.mark.parametrize("error_code", [None, "TestException"]) +queue_handler = QueueHandler(log_queue) +document_logger = logging.getLogger("maintenance_window") +document_logger.addHandler(queue_handler) + + +@pytest.mark.parametrize("error_code", [None, "ParamValidationError", "TestException"]) def test_create(make_stubber, error_code): ssm_client = boto3.client("ssm") ssm_stubber = make_stubber(ssm_client) @@ -40,6 +49,15 @@ def test_create(make_stubber, error_code): ) assert maintenance_window_wrapper.window_id == window_id assert maintenance_window_wrapper.name == name + elif error_code == "ParamValidationError": + with pytest.raises(ClientError) as exc_info: + maintenance_window_wrapper.create( + name, schedule, duration, cutoff, allow_unassociated_targets + ) + assert exc_info.value.response["Error"]["Code"] == error_code + assert ( + log_queue.qsize() > 0 and "validation error" in log_queue.get().getMessage() + ) else: with pytest.raises(ClientError) as exc_info: maintenance_window_wrapper.create(