1
0
Эх сурвалжийг харах

Merge pull request #1732 from Avaiga/feature/try-catch-the-upload_checker-call

Feature - Add try catch around the upload_checker call
Đỗ Trường Giang 8 сар өмнө
parent
commit
041789c9c8

+ 10 - 1
taipy/core/data/_file_datanode_mixin.py

@@ -157,7 +157,16 @@ class _FileDataNodeMixin(object):
             return reason_collection
 
         if upload_checker is not None:
-            if not upload_checker(upload_path.name, upload_data):
+            try:
+                can_upload = upload_checker(upload_path.name, upload_data)
+            except Exception as err:
+                self.__logger.error(
+                    f"Error while checking if {upload_path.name} can be uploaded to data node {self.id}"  # type: ignore[attr-defined]
+                    f" using the upload checker {upload_checker.__name__}: {err}"
+                )
+                can_upload = False
+
+            if not can_upload:
                 reason_collection._add_reason(self.id, InvalidUploadFile(upload_path.name, self.id))  # type: ignore[attr-defined]
                 return reason_collection
 

+ 14 - 0
tests/core/data/test_csv_data_node.py

@@ -249,6 +249,20 @@ class TestCSVDataNode:
         assert dn.last_edit_date > old_last_edit_date
         assert dn.path == old_csv_path  # The path of the dn should not change
 
+    def test_upload_with_upload_check_with_exception(self, csv_file, tmpdir_factory, caplog):
+        old_csv_path = tmpdir_factory.mktemp("data").join("df.csv").strpath
+        dn = CSVDataNode("foo", Scope.SCENARIO, properties={"path": old_csv_path, "exposed_type": "pandas"})
+
+        def check_with_exception(upload_path, upload_data):
+            raise Exception("An error with check_with_exception")
+
+        reasons = dn._upload(csv_file, upload_checker=check_with_exception)
+        assert bool(reasons) is False
+        assert (
+            f"Error while checking if df.csv can be uploaded to data node {dn.id} using "
+            "the upload checker check_with_exception: An error with check_with_exception" in caplog.text
+        )
+
     def test_upload_with_upload_check_pandas(self, csv_file, tmpdir_factory):
         old_csv_path = tmpdir_factory.mktemp("data").join("df.csv").strpath
         old_data = pd.DataFrame([{"a": 0, "b": 1, "c": 2}, {"a": 3, "b": 4, "c": 5}])