Skip to content

feat(batch): raise exception for invalid batch event #6088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 17, 2025

Conversation

anafalcao
Copy link
Contributor

@anafalcao anafalcao commented Feb 12, 2025

Issue number: #2394

Summary

Changes

This PR adds fail fast error handling for unexpected batch types in the Batch Processing utility.

  • Added UnexpectedBatchTypeError to handle invalid batch types in process_partial_response and async_process_partial_response
  • Error messaging to indicate valid event source types:
    Unexpected batch event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams

User experience

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.batch import (
    BatchProcessor, 
    EventType,
    process_partial_response,
    UnexpectedBatchTypeError
)

logger = Logger()

def record_handler(record):
    return record["body"]

def handler(event, context):
    processor = BatchProcessor(event_type=EventType.SQS)
    
    try:
        return process_partial_response(
            event=event,
            record_handler=record_handler,
            processor=processor
        )
    except UnexpectedBatchTypeError as e:
        # "Unexpected batch event type. Possible values are: SQS, KinesisDataStreams, DynamoDBStreams"
        logger.error(f"Invalid batch event structure: {e}")
        raise

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added the tests label Feb 12, 2025
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 12, 2025
@anafalcao anafalcao self-assigned this Feb 12, 2025
@github-actions github-actions bot added the feature New feature or functionality label Feb 12, 2025
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.25%. Comparing base (6e2171b) to head (293bb2c).
Report is 6 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6088   +/-   ##
========================================
  Coverage    96.25%   96.25%           
========================================
  Files          234      234           
  Lines        11140    11147    +7     
  Branches       822      824    +2     
========================================
+ Hits         10723    10730    +7     
  Misses         327      327           
  Partials        90       90           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anafalcao anafalcao marked this pull request as ready for review February 12, 2025 20:37
@anafalcao anafalcao requested a review from a team February 12, 2025 20:37
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @anafalcao! Thanks for working in this PR! I just left some comments before we merge this.

Please also leave a message in the original issue saying that we will raise an exception because we want to match the TypeScript version and have the same behavior in the API.

Also, in the "User Experience" section, add a complete example of the experience, including the try/except block, so that it's easy to copy when we write the release notes.

@anafalcao
Copy link
Contributor Author

Hi @leandrodamascena ! Thank you for the feedback. I worked on them in this last commit, and updated the User Experience section for a more complete example.
I'll leave a message in the original issue.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVED!

@leandrodamascena leandrodamascena merged commit 4ad37b5 into develop Feb 17, 2025
8 of 9 checks passed
@leandrodamascena leandrodamascena deleted the batchevent branch February 17, 2025 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Batch processor should fail early if event payload is incorrect
2 participants