Skip to content

Exception handling in batch processing doesn't work as expected #275

Closed
@pcolazurdo

Description

@pcolazurdo

In this code:

processor = PartialSQSProcessor(suppress_exception=False)
with processor(records, record_handler) as proc:
    result = proc.process()

If there is an exception raised inside the record_handler function, I expect the exception to be raised in the function and get some details about what caused the exception. The code in https://github.com/awslabs/aws-lambda-powertools-python/blob/e104bfb5dcedf7b52d4a7925ae446ff719b8c110/aws_lambda_powertools/utilities/batch/sqs.py#L126 tries to be smart about it and raise a list of all the exceptions in the batch, but this doesn't work as expected, as the Exception code is not smart enough to show a list of exceptions. The actual result looks like this:

Traceback (most recent call last):
  File "/var/task/aws_lambda_powertools/logging/logger.py", line 270, in decorate
    return lambda_handler(event, context)
  File "/var/task/aws_lambda_powertools/tracing/tracer.py", line 303, in decorate
    response = lambda_handler(event, context, **kwargs)
  File "/var/task/aws_lambda_powertools/metrics/metrics.py", line 144, in decorate
    response = lambda_handler(event, context)
  File "/var/task/app.py", line 179, in lambda_handler
    result = proc.process()  # Returns a list of all results from record_handler
  File "/var/task/aws_lambda_powertools/utilities/batch/base.py", line 58, in __exit__
    self._clean()
  File "/var/task/aws_lambda_powertools/utilities/batch/sqs.py", line 126, in _clean
    raise SQSBatchProcessingError(list(self.exceptions))

What were you trying to accomplish?

Expected Behavior

I would except the Exception to be raised in a manner that all the exceptions are shown together.
Also, reviewing the code I see there is a fail_message property that doesn't seem to be exposed anywhere. Is there a plan for this? This may be very useful in some cases.

Current Behavior

Please review the Stack trace above

Possible Solution

I think re raising an exception may need some extra work like what is commented here: https://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html

Steps to Reproduce (for bugs)

Raise an exception inside a function called from the record handler. Without debug mode, you can't see the details of this exception.

Environment

  • Powertools version used: aws-lambda-powertools-1.10.1
  • Packaging format (Layers, PyPi): PyPi
  • AWS Lambda function runtime: Python 3.8
  • Debugging logs

How to enable debug mode**

LogStream&Time:13.430000 START RequestId: fc69d380-4601-5cf1-b606-8b044590e830 Version: $LATEST
LogStream&Time:13.769000 2021-01-21 22:34:13,769 aws_lambda_powertools.utilities.batch.base [DEBUG] Record processing exception: 'Metadata' object has no attribute '_sk'
LogStream&Time:13.769000 2021-01-21 22:34:13,769 aws_lambda_powertools.utilities.batch.sqs [DEBUG] 1 records failed processing, raising exception
LogStream&Time:13.949000 aws_lambda_powertools.utilities.batch.exceptions.SQSBatchProcessingError: [AttributeError("'Metadata' object has no attribute '_sk'")]
LogStream&Time:13.950000 [ERROR] SQSBatchProcessingError: [AttributeError("'Metadata' object has no attribute '_sk'")]
Traceback (most recent call last):
  File "/var/task/aws_lambda_powertools/logging/logger.py", line 270, in decorate
    return lambda_handler(event, context)
  File "/var/task/aws_lambda_powertools/tracing/tracer.py", line 303, in decorate
    response = lambda_handler(event, context, **kwargs)
  File "/var/task/aws_lambda_powertools/metrics/metrics.py", line 144, in decorate
    response = lambda_handler(event, context)
  File "/var/task/app.py", line 183, in lambda_handler
    result = proc.process()  # Returns a list of all results from record_handler
  File "/var/task/aws_lambda_powertools/utilities/batch/base.py", line 58, in __exit__
    self._clean()
  File "/var/task/aws_lambda_powertools/utilities/batch/sqs.py", line 126, in _clean
    raise SQSBatchProcessingError(list(self.exceptions))
LogStream&Time:14.008000 END RequestId: fc69d380-4601-5cf1-b606-8b044590e830
LogStream&Time:14.008000 REPORT RequestId: fc69d380-4601-5cf1-b606-8b044590e830  Duration: 343.54 ms     Billed Duration: 344 ms    Memory Size: 128 MB     Max Memory Used: 105 MB Init Duration: 1149.70 ms
XRAY TraceId: 1-600a0164-4d5d5e616bb97d8a4e3f804b       SegmentId: 1972c55e04b1b0f3     Sampled: true

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions