Skip to content

Commit

Permalink
Merge pull request #1732 from Avaiga/feature/try-catch-the-upload_che…
Browse files Browse the repository at this point in the history
…cker-call

Feature - Add try catch around the upload_checker call
  • Loading branch information
trgiangdo authored Sep 6, 2024
2 parents e2d1f61 + d199f3c commit 041789c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion taipy/core/data/_file_datanode_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ def _upload(self, path: str, upload_checker: Optional[Callable[[str, Any], bool]
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

Expand Down
14 changes: 14 additions & 0 deletions tests/core/data/test_csv_data_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ def test_upload(self, csv_file, tmpdir_factory):
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}])
Expand Down

0 comments on commit 041789c

Please sign in to comment.