Skip to content

Commit

Permalink
feat: cleaner error messages for Python filters (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruelaneo authored Jan 8, 2025
2 parents f589859 + dc917c3 commit 8c658fc
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/python/src/armonik/common/filter/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def _sanitize_value(self, value: Any) -> Any:
"""
if self.__class__.value_type_ is None or isinstance(value, self.__class__.value_type_):
return value
msg = f"Expected value type {str(self.__class__.value_type_)} for field {str(self.field)}, got {str(type(value))} instead"
msg = f"Expected value type '{self.__class__.value_type_.__name__}' for field '{self.field_name}', got '{value.__class__.__name__}' instead"
raise FilterError(None, msg)

def _change_operation(
Expand Down Expand Up @@ -416,7 +416,7 @@ def _change_operation(
raise FilterError(self, "Cannot apply operator to a filter combination")
raise FilterError(self, "Cannot apply operator to an already defined filter")
if operator is None or self._is_disjunction():
msg = f"Operator {operator_str} is not available for {self.__class__.__name__}"
msg = f"Operator {operator_str} is not available for {self.__class__.__name__}."
raise FilterError(self, msg)
return self.__class__(
self.field,
Expand Down Expand Up @@ -457,6 +457,10 @@ def to_dict(self) -> Dict[str, Any]:
else {}
)

@property
def field_name(self) -> str:
return str(self.field).replace(" ", "").split("\n")[1].split("ENUM_FIELD_")[1].lower()

def __str__(self) -> str:
return json.dumps(self.to_dict())

Expand Down Expand Up @@ -640,8 +644,8 @@ def _sanitize_value(self, value: Any) -> Any:
if isinstance(value, Timestamp):
return value
msg = (
f"Expected value type {datetime.__class__.__name__} or {Timestamp.__class__.__name__}"
f"for field {str(self.field)}, got {str(type(value))} instead"
f"Expected value type '{datetime.__name__}' or '{Timestamp.__name__}' "
f"for field '{self.field_name}', got '{type(value).__name__}' instead"
)
raise FilterError(self, msg)

Expand Down Expand Up @@ -788,8 +792,8 @@ def _sanitize_value(self, value: Any) -> Any:
if isinstance(value, Duration):
return value
msg = (
f"Expected value type {timedelta.__class__.__name__} or {Duration.__class__.__name__}"
f"for field {str(self.field)}, got {str(type(value))} instead"
f"Expected value type '{timedelta.__name__}' or '{Duration.__name__}' "
f"for field '{self.field_name}', got '{type(value).__name__}' instead."
)
raise FilterError(self, msg)

Expand Down

0 comments on commit 8c658fc

Please sign in to comment.