Skip to content

Commit cbd49f8

Browse files
committed
Changed exception handling lambda to use isinstance rather than is type
1 parent 0f9ef87 commit cbd49f8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
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):

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)