Skip to content

Commit

Permalink
feat: removal of repeated code in input validation
Browse files Browse the repository at this point in the history
Refs: 251
  • Loading branch information
pc532627 committed Dec 5, 2023
1 parent 0c1e914 commit ba21a32
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions coreax/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,14 @@ def cast_as_type(x: Any, object_name: str, type_caster: Callable) -> Any:
"""
try:
return type_caster(x)
except TypeError as e:
error_text = (
f"{object_name} cannot be cast using {type_caster}. " f"Given value {x}.\n"
)
except (TypeError, ValueError) as e:
error_text = f"{object_name} cannot be cast using {type_caster}. \n"
if hasattr(e, "message"):
error_text += e.message
else:
error_text += str(e)
raise TypeError(error_text)
except ValueError as e:
error_text = (
f"{object_name} cannot be cast using {type_caster}. " f"Given value {x}.\n"
)
if hasattr(e, "message"):
error_text += e.message

if isinstance(e, TypeError):
raise TypeError(error_text)
else:
error_text += str(e)
raise ValueError(error_text)
raise ValueError(error_text)

0 comments on commit ba21a32

Please sign in to comment.