Skip to content

fix(batch): resolve use of ValidationError in batch #2157

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 3 commits into from
Apr 21, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions aws_lambda_powertools/utilities/batch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
KinesisStreamRecord,
)
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
from aws_lambda_powertools.utilities.parser import ValidationError
from aws_lambda_powertools.utilities.typing import LambdaContext

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

return self.success_handler(record=record, result=result)
except ValidationError:
return self._register_model_validation_error_record(record)
except Exception:
except Exception as exc:
# NOTE: Pydantic is an optional dependency, but when used and a poison pill scenario happens
# we need to handle that exception differently.
# We check for a public attr in validation errors coming from Pydantic exceptions (subclass or not)
# and we compare if it's coming from the same model that trigger the exception in the first place
model = getattr(exc, "model", None)
if model == self.model:
return self._register_model_validation_error_record(record)

return self.failure_handler(record=data, exception=sys.exc_info())


Expand Down Expand Up @@ -634,7 +639,8 @@ async def _async_process_record(self, record: dict) -> Union[SuccessResponse, Fa
result = await self.handler(record=data)

return self.success_handler(record=record, result=result)
except ValidationError:
return self._register_model_validation_error_record(record)
except Exception:
except Exception as exc:
if exc.__class__.__name__ == "ValidationError":
return self._register_model_validation_error_record(record)

return self.failure_handler(record=data, exception=sys.exc_info())