Skip to content

feat(batch): add option to not raise BatchProcessingError exception when the entire batch fails #4719

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

Conversation

leandrodamascena
Copy link
Contributor

@leandrodamascena leandrodamascena commented Jul 9, 2024

Issue number: #4691

Summary

Changes

Currently, when using the Batch Processor utility, if all the records in a batch are marked as failed, the utility raise a BatchProcessingError exception. However, there can be scenarios where we do not want to throw an error for this condition.

This PR adds a raise_on_entire_batch_failure option, giving users the ability to bypass the BatchProcessingError when the entire batch fails.

User experience

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.batch import (
    BatchProcessor,
    EventType,
    process_partial_response,
)
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
from aws_lambda_powertools.utilities.typing import LambdaContext

processor = BatchProcessor(event_type=EventType.SQS, raise_on_entire_batch_failure=False)
tracer = Tracer()
logger = Logger()


@tracer.capture_method
def record_handler(record: SQSRecord):
    payload: str = record.json_body  # if json string data, otherwise record.body for str
    logger.info(payload)


@logger.inject_lambda_context
@tracer.capture_lambda_handler
def lambda_handler(event, context: LambdaContext):
    return process_partial_response(
        event=event,
        record_handler=record_handler,
        processor=processor,
        context=context,
    )

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.

@leandrodamascena leandrodamascena requested a review from a team July 9, 2024 17:18
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation tests labels Jul 9, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 9, 2024
@leandrodamascena leandrodamascena linked an issue Jul 9, 2024 that may be closed by this pull request
2 tasks
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Jul 9, 2024
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.42%. Comparing base (e17c1cb) to head (7a44ba0).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4719   +/-   ##
========================================
  Coverage    96.42%   96.42%           
========================================
  Files          223      223           
  Lines        10755    10756    +1     
  Branches      2001     2001           
========================================
+ Hits         10371    10372    +1     
  Misses         270      270           
  Partials       114      114           

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

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 9, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 9, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 10, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 10, 2024
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Hi @leandrodamascena, thank you for the PR.

My only comment is related to the name of the new parameter. I would suggest consider renaming it to raise_on_entire_batch_failure both for consistency (i.e. other things are called PartialItemFailureResponse and batchItemFailures) and for correctness.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 10, 2024
@leandrodamascena
Copy link
Contributor Author

Hi @leandrodamascena, thank you for the PR.

My only comment is related to the name of the new parameter. I would suggest consider renaming it to raise_on_entire_batch_failure both for consistency (i.e. other things are called PartialItemFailureResponse and batchItemFailures) and for correctness.

Great point @dreamorosi! I changed the name of the parameter.

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 10, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 10, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 10, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 10, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 10, 2024
dreamorosi
dreamorosi previously approved these changes Jul 10, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 10, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 10, 2024
@leandrodamascena leandrodamascena merged commit 14b44ce into aws-powertools:develop Jul 10, 2024
17 checks passed
@leandrodamascena leandrodamascena deleted the feat/batch-processor-no-exception branch July 10, 2024 13:29
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jul 11, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jul 11, 2024
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/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: ability to not throw error on full batch failure
2 participants