Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lord-haffi committed Jan 13, 2024
1 parent 16f8aa5 commit d77608c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/pvframework/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ def __init__(
provided_params = validation_manager.info.running_tasks[
validation_manager.info.tasks[mapped_validator]
].current_provided_params
data_set_str = str(data_set)
if len(data_set_str) > 80:
data_set_str = data_set_str[:77] + "..."
message = (
f"{error_id}: {message_detail}\n"
f"\tDataSet: {data_set})\n"
f"{error_id}, {type(cause).__name__}: {message_detail}\n"
f"\tDataSet: {data_set_str}\n"
f"\tError ID: {error_id}\n"
f"\tError type: {type(cause).__name__}\n"
f"\tValidator function: {mapped_validator.name}"
)
if provided_params is not None:
Expand All @@ -149,7 +153,7 @@ def __init__(
self.cause = cause
self.data_set = data_set
self.mapped_validator = mapped_validator
self.validator_set = validation_manager
self.validation_manager = validation_manager
self.error_id = error_id
self.message_detail = message_detail
self.provided_params = provided_params
Expand Down
20 changes: 12 additions & 8 deletions src/pvframework/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,22 @@ async def _are_params_ok(
custom_error_id=_CustomErrorIDS.PARAM_PROVIDER_ERRORED,
)
return False
try:
self.info.current_provided_params = params_or_exc
for param_name, param in params_or_exc.items():
self.info.current_provided_params = params_or_exc
for param_name, param in params_or_exc.items():
try:
check_type(
param.value,
mapped_validator.validator.signature.parameters[param_name].annotation,
)
except TypeCheckError as error:
await self.info.error_handler.catch(
str(error), error, mapped_validator, self, custom_error_id=_CustomErrorIDS.PARAM_TYPE_MISMATCH
)
return False
except TypeCheckError as error:
await self.info.error_handler.catch(
f"{param.param_id}: {error}",
error,
mapped_validator,
self,
custom_error_id=_CustomErrorIDS.PARAM_TYPE_MISMATCH,
)
return False
return True

async def _execute_async_validator(
Expand Down
2 changes: 1 addition & 1 deletion src/pvframework/mapped_validators/path_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def provide(self, data_set: DataSetT) -> Generator[Parameters[DataSetT] | Except
provided = True
except AttributeError as error:
if param_name in self.validator.required_param_names:
query_error = AttributeError(f"{attr_path} not provided")
query_error = AttributeError(f"{attr_path}: value not provided")
query_error.__cause__ = error
yield query_error
skip = True
Expand Down
4 changes: 2 additions & 2 deletions src/pvframework/mapped_validators/query_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ def _iter_func(data_set: DataSetT) -> Iterator[IteratorReturnTypeWithException]:
sub_el: Any = required_field(parent_el[0], attr_path, Any)
yield sub_el, f"{parent_el[1]}.{attr_path}"
except AttributeError as error:
query_error = AttributeError(f"{parent_el[1]}.{attr_path} not provided")
query_error = AttributeError(f"{parent_el[1]}.{attr_path}: value not provided")
query_error.__cause__ = error
yield query_error
else:
try:
sub_el = required_field(data_set, attr_path, Any)
yield sub_el, attr_path
except AttributeError as error:
query_error = AttributeError(f"{attr_path} not provided")
query_error = AttributeError(f"{attr_path}: value not provided")
query_error.__cause__ = error
yield query_error

Expand Down
4 changes: 2 additions & 2 deletions unittests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ async def test_unprovided_but_required(self):
validation_summary = await validation_manager.validate(dataset_instance)

assert validation_summary.num_errors_total == 1
assert "z.z not provided" in str(validation_summary.all_errors[0])
assert "z.z: value not provided" in str(validation_summary.all_errors[0])

async def test_multiple_validator_registration(self):
global finishing_order
Expand Down Expand Up @@ -541,7 +541,7 @@ async def test_special_data_set_errors(self):
SpecialDataSet.model_construct(x=special_dataset_instance.x)
)
assert validation_summary.num_errors_total == 1
assert "y not provided" in str(validation_summary.all_errors[0])
assert "y: value not provided" in str(validation_summary.all_errors[0])
assert len(finishing_order) == 3
assert all(el == check_special_data_set_optional for el in finishing_order)

Expand Down

0 comments on commit d77608c

Please sign in to comment.