From a07a66cf378af3baec9135739f3e42d977393f79 Mon Sep 17 00:00:00 2001 From: Steinthor Palsson Date: Tue, 19 Sep 2023 13:25:31 -0400 Subject: [PATCH] Imrpvoe validation error message --- dlt/extract/exceptions.py | 7 +++++-- dlt/extract/validation.py | 5 ++++- tests/extract/test_validation.py | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dlt/extract/exceptions.py b/dlt/extract/exceptions.py index 71aaffdeb2..4a4b17967d 100644 --- a/dlt/extract/exceptions.py +++ b/dlt/extract/exceptions.py @@ -3,6 +3,7 @@ from dlt.common.exceptions import DltException from dlt.common.utils import get_callable_name +from dlt.extract.typing import ValidateItem, TDataItems class ExtractorException(DltException): @@ -262,6 +263,8 @@ def __init__(self, cursor_path: str) -> None: class ValidationError(ValueError, DltException): - def __init__(self, original_exception: Exception) ->None: + def __init__(self, validator: ValidateItem, data_item: TDataItems, original_exception: Exception) ->None: self.original_exception = original_exception - super().__init__(f"Schema validation failed: {original_exception}") + self.validator = validator + self.data_item = data_item + super().__init__(f"Extracted data item could not be validated with {validator}. Original message: {original_exception}") diff --git a/dlt/extract/validation.py b/dlt/extract/validation.py index 0a29bf107f..c8e30d0eb2 100644 --- a/dlt/extract/validation.py +++ b/dlt/extract/validation.py @@ -34,7 +34,10 @@ def __call__(self, item: TDataItems, meta: Any = None) -> Union[_TPydanticModel, return self.list_model(items=item).items # type: ignore[attr-defined, no-any-return] return self.model.parse_obj(item) except PydanticValidationError as e: - raise ValidationError(e) from e + raise ValidationError(self, item, e) from e + + def __str__(self, *args: Any, **kwargs: Any) -> str: + return f"PydanticValidator(model={self.model.__qualname__})" def get_column_validator(columns: TTableHintTemplate[TAnySchemaColumns]) -> Optional[ValidateItem]: diff --git a/tests/extract/test_validation.py b/tests/extract/test_validation.py index 855bed4326..64e06bcecc 100644 --- a/tests/extract/test_validation.py +++ b/tests/extract/test_validation.py @@ -101,7 +101,7 @@ class AnotherModel(BaseModel): assert len(steps) == 2 assert isinstance(steps[-1], ValidateItem) - assert steps[-1].model is AnotherModel + assert steps[-1].model is AnotherModel # type: ignore[attr-defined] @pytest.mark.parametrize("yield_list", [True, False]) @@ -149,3 +149,4 @@ def some_data() -> t.Iterator[TDataItems]: list(some_data()) assert isinstance(exinfo.value.__cause__, ValidationError) + assert str(PydanticValidator(SimpleModel)) in str(exinfo.value)