Skip to content

Commit

Permalink
adding exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws committed Jun 27, 2024
1 parent 23c6969 commit 8b23c2f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
10 changes: 9 additions & 1 deletion python/example_code/ssm/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion python/example_code/ssm/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# snippet-start:[python.example_code.ssm.Hello]
import boto3
from botocore.exceptions import ClientError


def hello_systems_manager(ssm_client):
Expand All @@ -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]
16 changes: 15 additions & 1 deletion python/example_code/ssm/maintenance_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

import boto3
from botocore.exceptions import ClientError
from botocore.exceptions import ClientError, ParamValidationError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 3 additions & 13 deletions python/example_code/ssm/test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 20 additions & 2 deletions python/example_code/ssm/test/test_maintenance_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 8b23c2f

Please sign in to comment.