Skip to content

Commit bd30e96

Browse files
committed
feat(batch): raise exception for invalid batch event
1 parent cd9ca6f commit bd30e96

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

aws_lambda_powertools/utilities/batch/decorators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
BatchProcessor,
1313
EventType,
1414
)
15+
from aws_lambda_powertools.utilities.batch.exceptions import UnexpectedBatchTypeError
1516
from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning
1617

1718
if TYPE_CHECKING:
@@ -204,6 +205,9 @@ def handler(event, context):
204205
"""
205206
try:
206207
records: list[dict] = event.get("Records", [])
208+
if not records or not isinstance(records, list):
209+
raise UnexpectedBatchTypeError
210+
207211
except AttributeError:
208212
event_types = ", ".join(list(EventType.__members__))
209213
docs = "https://docs.powertools.aws.dev/lambda/python/latest/utilities/batch/#processing-messages-from-sqs" # noqa: E501 # long-line
@@ -268,6 +272,9 @@ def handler(event, context):
268272
"""
269273
try:
270274
records: list[dict] = event.get("Records", [])
275+
if not records or not isinstance(records, list):
276+
raise UnexpectedBatchTypeError
277+
271278
except AttributeError:
272279
event_types = ", ".join(list(EventType.__members__))
273280
docs = "https://docs.powertools.aws.dev/lambda/python/latest/utilities/batch/#processing-messages-from-sqs" # noqa: E501 # long-line

aws_lambda_powertools/utilities/batch/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def __str__(self):
3838
return self.format_exceptions(parent_exception_str)
3939

4040

41+
class UnexpectedBatchTypeError(BatchProcessingError):
42+
"""Error thrown by the Batch Processing utility when a partial processor receives an unexpected batch type"""
43+
44+
def __init__(self):
45+
msg = "Unexpected batch type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams"
46+
super().__init__(msg)
47+
self.name = "UnexpectedBatchTypeError"
48+
49+
4150
class SQSFifoCircuitBreakerError(Exception):
4251
"""
4352
Signals a record not processed due to the SQS FIFO processing being interrupted

tests/functional/batch/required_dependencies/test_utilities_batch.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
batch_processor,
1616
process_partial_response,
1717
)
18-
from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError
18+
from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError, UnexpectedBatchTypeError
1919
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
2020
DynamoDBRecord,
2121
)
@@ -708,3 +708,69 @@ def test_async_process_partial_response_invalid_input(async_record_handler: Call
708708
# WHEN/THEN
709709
with pytest.raises(ValueError):
710710
async_process_partial_response(batch, record_handler, processor)
711+
712+
713+
@pytest.mark.parametrize(
714+
"event",
715+
[
716+
{},
717+
{"Records": None},
718+
{"Records": "not a list"},
719+
],
720+
)
721+
def test_process_partial_response_raises_unexpected_batch_type(event, record_handler):
722+
# GIVEN a batch processor configured for SQS events
723+
processor = BatchProcessor(event_type=EventType.SQS)
724+
725+
# WHEN processing an event with invalid Records
726+
with pytest.raises(UnexpectedBatchTypeError) as exc_info:
727+
process_partial_response(
728+
event=event,
729+
record_handler=record_handler,
730+
processor=processor,
731+
)
732+
733+
# THEN the correct error message is raised
734+
assert "Unexpected batch type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str(exc_info.value)
735+
736+
737+
@pytest.mark.asyncio
738+
@pytest.mark.parametrize(
739+
"event",
740+
[
741+
{},
742+
{"Records": None},
743+
{"Records": "not a list"},
744+
],
745+
)
746+
async def test_async_process_partial_response_raises_unexpected_batch_type(event, async_record_handler):
747+
# GIVEN a batch processor configured for SQS events
748+
processor = BatchProcessor(event_type=EventType.SQS)
749+
750+
# WHEN processing an event with invalid Records asynchronously
751+
with pytest.raises(UnexpectedBatchTypeError) as exc_info:
752+
await async_process_partial_response(
753+
event=event,
754+
record_handler=async_record_handler,
755+
processor=processor,
756+
)
757+
758+
# THEN the correct error message is raised
759+
assert "Unexpected batch type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str(exc_info.value)
760+
761+
762+
def test_process_partial_response_should_not_raise_unexpected_batch_type(record_handler):
763+
# GIVEN a valid SQS event structure
764+
event = {"Records": [{"messageId": "1", "body": "test"}]}
765+
processor = BatchProcessor(event_type=EventType.SQS)
766+
767+
# WHEN processing the event
768+
try:
769+
process_partial_response(
770+
event=event,
771+
record_handler=record_handler,
772+
processor=processor,
773+
)
774+
except UnexpectedBatchTypeError:
775+
# THEN no UnexpectedBatchTypeError should be raised
776+
pytest.fail("UnexpectedBatchTypeError was raised with a valid event structure!")

0 commit comments

Comments
 (0)