Skip to content

Commit b683563

Browse files
Merge pull request #111 from gridsmartercities/fix_exception_handling
Changed exception handling lambda to use isinstance rather than is type
2 parents 0f9ef87 + 3e0ffb6 commit b683563

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

aws_lambda_decorators/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def decorator(func):
136136
def wrapper(*args, **kwargs):
137137
try:
138138
return func(*args, **kwargs)
139-
except tuple([handler.exception for handler in handlers]) as ex: # noqa: pylint - catching-non-exception
140-
failed_handler = [handler for handler in handlers if handler.exception is type(ex)][0]
139+
except tuple(handler.exception for handler in handlers) as ex: # noqa: pylint - catching-non-exception
140+
failed_handler = [handler for handler in handlers if isinstance(ex, handler.exception)][0]
141141
message = failed_handler.friendly_message
142142

143143
if message and str(ex):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
LONG_DESCRIPTION = open("README.md").read()
44

55
setup(name="aws-lambda-decorators",
6-
version="0.46",
6+
version="0.47",
77
description="A set of python decorators to simplify aws python lambda development",
88
long_description=LONG_DESCRIPTION,
99
long_description_content_type="text/markdown",

tests/test_decorators.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,20 @@ def handler():
459459

460460
mock_logger.error.assert_called_once_with("%s: %s", "error", "'blank'")
461461

462+
@patch("aws_lambda_decorators.decorators.LOGGER")
463+
def test_exception_handler_raises_exception_with_inherited_exception(self, mock_logger):
464+
465+
@handle_exceptions(handlers=[ExceptionHandler(Exception)])
466+
def handler():
467+
raise KeyError("blank")
468+
469+
response = handler()
470+
471+
self.assertEqual(400, response["statusCode"])
472+
self.assertTrue("blank" in response["body"])
473+
474+
mock_logger.error.assert_called_once_with("'blank'")
475+
462476
@patch("aws_lambda_decorators.decorators.LOGGER")
463477
def test_log_decorator_can_log_params(self, mock_logger): # noqa: pylint - no-self-use
464478

0 commit comments

Comments
 (0)