|
26 | 26 | KinesisStreamRecord,
|
27 | 27 | )
|
28 | 28 | from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
|
29 |
| -from aws_lambda_powertools.utilities.parser import ValidationError |
30 | 29 | from aws_lambda_powertools.utilities.typing import LambdaContext
|
31 | 30 |
|
32 | 31 | logger = logging.getLogger(__name__)
|
@@ -496,9 +495,15 @@ def _process_record(self, record: dict) -> Union[SuccessResponse, FailureRespons
|
496 | 495 | result = self.handler(record=data)
|
497 | 496 |
|
498 | 497 | return self.success_handler(record=record, result=result)
|
499 |
| - except ValidationError: |
500 |
| - return self._register_model_validation_error_record(record) |
501 |
| - except Exception: |
| 498 | + except Exception as exc: |
| 499 | + # NOTE: Pydantic is an optional dependency, but when used and a poison pill scenario happens |
| 500 | + # we need to handle that exception differently. |
| 501 | + # We check for a public attr in validation errors coming from Pydantic exceptions (subclass or not) |
| 502 | + # and we compare if it's coming from the same model that trigger the exception in the first place |
| 503 | + model = getattr(exc, "model", None) |
| 504 | + if model == self.model: |
| 505 | + return self._register_model_validation_error_record(record) |
| 506 | + |
502 | 507 | return self.failure_handler(record=data, exception=sys.exc_info())
|
503 | 508 |
|
504 | 509 |
|
@@ -634,7 +639,13 @@ async def _async_process_record(self, record: dict) -> Union[SuccessResponse, Fa
|
634 | 639 | result = await self.handler(record=data)
|
635 | 640 |
|
636 | 641 | return self.success_handler(record=record, result=result)
|
637 |
| - except ValidationError: |
638 |
| - return self._register_model_validation_error_record(record) |
639 |
| - except Exception: |
| 642 | + except Exception as exc: |
| 643 | + # NOTE: Pydantic is an optional dependency, but when used and a poison pill scenario happens |
| 644 | + # we need to handle that exception differently. |
| 645 | + # We check for a public attr in validation errors coming from Pydantic exceptions (subclass or not) |
| 646 | + # and we compare if it's coming from the same model that trigger the exception in the first place |
| 647 | + model = getattr(exc, "model", None) |
| 648 | + if model == self.model: |
| 649 | + return self._register_model_validation_error_record(record) |
| 650 | + |
640 | 651 | return self.failure_handler(record=data, exception=sys.exc_info())
|
0 commit comments