Skip to content

Commit 4ad37b5

Browse files
feat(batch): raise exception for invalid batch event (#6088)
* feat(batch): raise exception for invalid batch event * change msg error --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 04191b4 commit 4ad37b5

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

aws_lambda_powertools/utilities/batch/decorators.py

+11
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,11 @@ 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+
"Unexpected batch event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams",
211+
)
212+
207213
except AttributeError:
208214
event_types = ", ".join(list(EventType.__members__))
209215
docs = "https://docs.powertools.aws.dev/lambda/python/latest/utilities/batch/#processing-messages-from-sqs" # noqa: E501 # long-line
@@ -268,6 +274,11 @@ def handler(event, context):
268274
"""
269275
try:
270276
records: list[dict] = event.get("Records", [])
277+
if not records or not isinstance(records, list):
278+
raise UnexpectedBatchTypeError(
279+
"Unexpected batch event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams",
280+
)
281+
271282
except AttributeError:
272283
event_types = ", ".join(list(EventType.__members__))
273284
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

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ 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+
pass
45+
46+
4147
class SQSFifoCircuitBreakerError(Exception):
4248
"""
4349
Signals a record not processed due to the SQS FIFO processing being interrupted

tests/functional/batch/required_dependencies/test_utilities_batch.py

+54-1
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,56 @@ 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 event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str(
735+
exc_info.value,
736+
)
737+
738+
739+
@pytest.mark.asyncio
740+
@pytest.mark.parametrize(
741+
"event",
742+
[
743+
{},
744+
{"Records": None},
745+
{"Records": "not a list"},
746+
],
747+
)
748+
async def test_async_process_partial_response_raises_unexpected_batch_type(event, async_record_handler):
749+
# GIVEN a batch processor configured for SQS events
750+
processor = BatchProcessor(event_type=EventType.SQS)
751+
752+
# WHEN processing an event with invalid Records asynchronously
753+
with pytest.raises(UnexpectedBatchTypeError) as exc_info:
754+
await async_process_partial_response(
755+
event=event,
756+
record_handler=async_record_handler,
757+
processor=processor,
758+
)
759+
760+
# THEN the correct error message is raised
761+
assert "Unexpected batch event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams" in str(
762+
exc_info.value,
763+
)

0 commit comments

Comments
 (0)