Skip to content

Commit

Permalink
Imrpvoe validation error message
Browse files Browse the repository at this point in the history
  • Loading branch information
steinitzu committed Sep 19, 2023
1 parent 9cf59ee commit a07a66c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions dlt/extract/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}")
5 changes: 4 additions & 1 deletion dlt/extract/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
3 changes: 2 additions & 1 deletion tests/extract/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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)

0 comments on commit a07a66c

Please sign in to comment.