From d77608c1b73f59915262e005124618fbeae88ce7 Mon Sep 17 00:00:00 2001 From: Leon Haffmans Date: Sat, 13 Jan 2024 10:53:17 +0100 Subject: [PATCH] Improve error messages --- src/pvframework/errors.py | 10 +++++++--- src/pvframework/execution.py | 20 +++++++++++-------- src/pvframework/mapped_validators/path_map.py | 2 +- .../mapped_validators/query_map.py | 4 ++-- unittests/test_validation.py | 4 ++-- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/pvframework/errors.py b/src/pvframework/errors.py index 6f63b7d..e04ad8c 100644 --- a/src/pvframework/errors.py +++ b/src/pvframework/errors.py @@ -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: @@ -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 diff --git a/src/pvframework/execution.py b/src/pvframework/execution.py index 91492c6..b0df6c7 100644 --- a/src/pvframework/execution.py +++ b/src/pvframework/execution.py @@ -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( diff --git a/src/pvframework/mapped_validators/path_map.py b/src/pvframework/mapped_validators/path_map.py index 715f83a..1b35b74 100644 --- a/src/pvframework/mapped_validators/path_map.py +++ b/src/pvframework/mapped_validators/path_map.py @@ -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 diff --git a/src/pvframework/mapped_validators/query_map.py b/src/pvframework/mapped_validators/query_map.py index 13d1930..bc47cfb 100644 --- a/src/pvframework/mapped_validators/query_map.py +++ b/src/pvframework/mapped_validators/query_map.py @@ -92,7 +92,7 @@ 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: @@ -100,7 +100,7 @@ def _iter_func(data_set: DataSetT) -> Iterator[IteratorReturnTypeWithException]: 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 diff --git a/unittests/test_validation.py b/unittests/test_validation.py index 1fcdc35..1bf9207 100644 --- a/unittests/test_validation.py +++ b/unittests/test_validation.py @@ -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 @@ -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)