Skip to content

Commit cd5e1c6

Browse files
fix(batch): resolve use of ValidationError in batch (#2157)
Co-authored-by: heitorlessa <[email protected]>
1 parent d00425f commit cd5e1c6

File tree

1 file changed

+18
-7
lines changed
  • aws_lambda_powertools/utilities/batch

1 file changed

+18
-7
lines changed

aws_lambda_powertools/utilities/batch/base.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
KinesisStreamRecord,
2727
)
2828
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
29-
from aws_lambda_powertools.utilities.parser import ValidationError
3029
from aws_lambda_powertools.utilities.typing import LambdaContext
3130

3231
logger = logging.getLogger(__name__)
@@ -496,9 +495,15 @@ def _process_record(self, record: dict) -> Union[SuccessResponse, FailureRespons
496495
result = self.handler(record=data)
497496

498497
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+
502507
return self.failure_handler(record=data, exception=sys.exc_info())
503508

504509

@@ -634,7 +639,13 @@ async def _async_process_record(self, record: dict) -> Union[SuccessResponse, Fa
634639
result = await self.handler(record=data)
635640

636641
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+
640651
return self.failure_handler(record=data, exception=sys.exc_info())

0 commit comments

Comments
 (0)