From 65f2c5ac6ee82a5c707d87ed4c277132418c5a2d Mon Sep 17 00:00:00 2001 From: Ravi Kumar Pilla Date: Mon, 3 Feb 2025 13:29:40 -0600 Subject: [PATCH] add log (#2266) Signed-off-by: ravi_kumar_pilla --- .../integrations/kedro/lite_parser.py | 17 +++++++++++++---- .../test_integrations/test_lite_parser.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/package/kedro_viz/integrations/kedro/lite_parser.py b/package/kedro_viz/integrations/kedro/lite_parser.py index e3af8b42e6..aa531d2eea 100755 --- a/package/kedro_viz/integrations/kedro/lite_parser.py +++ b/package/kedro_viz/integrations/kedro/lite_parser.py @@ -240,9 +240,17 @@ def parse(self, target_path: Path) -> Union[Dict[str, Set[str]], None]: unresolved_imports: Dict[str, Set[str]] = {} if target_path.is_file(): - missing_dependencies = self._get_unresolved_imports(target_path) - if len(missing_dependencies) > 0: - unresolved_imports[str(target_path)] = missing_dependencies + try: + missing_dependencies = self._get_unresolved_imports(target_path) + if len(missing_dependencies) > 0: + unresolved_imports[str(target_path)] = missing_dependencies + except Exception as exc: # noqa: BLE001 + logger.error( + "An error occurred in LiteParser while mocking dependencies in %s : %s", + target_path, + exc, + ) + return unresolved_imports # handling directories @@ -263,7 +271,8 @@ def parse(self, target_path: Path) -> Union[Dict[str, Set[str]], None]: unresolved_imports[str(file_path)] = missing_dependencies except Exception as exc: # noqa: BLE001 # pragma: no cover logger.error( - "An error occurred in LiteParser while mocking dependencies : %s", + "An error occurred in LiteParser while mocking dependencies in %s : %s", + file_path, exc, ) continue diff --git a/package/tests/test_integrations/test_lite_parser.py b/package/tests/test_integrations/test_lite_parser.py index b3ae1eedea..7e55010b23 100644 --- a/package/tests/test_integrations/test_lite_parser.py +++ b/package/tests/test_integrations/test_lite_parser.py @@ -179,6 +179,25 @@ def test_file_parse(self, lite_parser, sample_project_path): assert unresolved_imports == {str(file_path): {"nonexistentmodule"}} + def test_parse_logs_error_on_exception(self, lite_parser, tmp_path, caplog): + file_path = Path(tmp_path / "mock_spaceflights/data_processing_non_utf.py") + file_path.parent.mkdir(parents=True, exist_ok=True) + + # Write non-UTF characters (e.g., using ISO-8859-1 encoding) + non_utf_content = "This is a test with non-UTF characters: é, ñ, ü" + + # Write the file in ISO-8859-1 encoding + with open(file_path, "w", encoding="ISO-8859-1") as f: + f.write(non_utf_content) + + assert file_path.exists() + + lite_parser.parse(file_path) + assert ( + f"An error occurred in LiteParser while mocking dependencies in {str(file_path)}" + in caplog.text + ) + def test_directory_parse(self, lite_parser, sample_project_path): unresolved_imports = lite_parser.parse(sample_project_path) expected_file_path = Path(