Skip to content

Commit

Permalink
Merge pull request #9122 from khoaguin/better-error-message-for-unsup…
Browse files Browse the repository at this point in the history
…ported-datatypes

Improve error message when uploading unsupported datatypes
  • Loading branch information
khoaguin authored Aug 12, 2024
2 parents f859665 + 782b3b8 commit f315b98
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/syft/src/syft/client/datasite_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..service.dataset.dataset import CreateDataset
from ..service.migration.object_migration_state import MigrationData
from ..service.response import SyftError
from ..service.response import SyftException
from ..service.response import SyftSuccess
from ..service.response import SyftWarning
from ..service.sync.diff_state import ResolvedSyncState
Expand Down Expand Up @@ -146,6 +147,8 @@ def upload_dataset(self, dataset: CreateDataset) -> SyftSuccess | SyftError:
res = twin._save_to_blob_storage(allow_empty=contains_empty)
if isinstance(res, SyftError):
return res
except SyftException as se:
return SyftError(message=f"{se}")
except Exception as e:
tqdm.write(f"Failed to create twin for {asset.name}. {e}")
return SyftError(message=f"Failed to create twin. {e}")
Expand Down
13 changes: 13 additions & 0 deletions packages/syft/src/syft/service/action/action_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from ...types.syft_object import SYFT_OBJECT_VERSION_1
from ...types.syft_object import SyftBaseObject
from ...types.syft_object import SyftObject
from ...types.syft_object_registry import SyftObjectRegistry
from ...types.syncable_object import SyncableSyftObject
from ...types.uid import LineageID
from ...types.uid import UID
Expand Down Expand Up @@ -1412,6 +1413,18 @@ def from_obj(
if id is not None and syft_lineage_id is not None and id != syft_lineage_id.id:
raise ValueError("UID and LineageID should match")

# check if the object's type is supported
try:
canonical_name, version = SyftObjectRegistry.get_canonical_name_version(
syft_action_data
)
except Exception:
obj_type = type(syft_action_data)
raise SyftException(
f"Error when creating action object for {syft_action_data}.\n"
f"Unsupported data type: '{obj_type.__module__}.{obj_type.__name__}'"
)

action_type = action_type_for_object(syft_action_data)
action_object = action_type(syft_action_data_cache=syft_action_data)
action_object.syft_blob_storage_entry_id = syft_blob_storage_entry_id
Expand Down
6 changes: 5 additions & 1 deletion packages/syft/src/syft/service/blob_storage/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any

# relative
from ...service.response import SyftException
from ...util.util import get_mb_serialized_size
from ..metadata.server_metadata import ServerMetadata
from ..metadata.server_metadata import ServerMetadataJSON
Expand All @@ -16,4 +17,7 @@ def min_size_for_blob_storage_upload(
def can_upload_to_blob_storage(
data: Any, metadata: ServerMetadata | ServerMetadataJSON
) -> bool:
return get_mb_serialized_size(data) >= min_size_for_blob_storage_upload(metadata)
serialized_size = get_mb_serialized_size(data)
if serialized_size.is_err():
raise SyftException(f"{serialized_size.err()}")
return serialized_size.ok() >= min_size_for_blob_storage_upload(metadata)
4 changes: 3 additions & 1 deletion packages/syft/src/syft/types/syft_object_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def get_canonical_name_version(cls, obj: Any) -> tuple[str, int]:
obj_type = type(obj)
if obj_type in cls.__type_to_canonical_name__:
return cls.__type_to_canonical_name__[obj_type]
raise ValueError(f"Could not find canonical name for {obj}")
raise ValueError(
f"Could not find canonical name for '{obj_type.__module__}.{obj_type.__name__}'"
)

@classmethod
def get_serde_properties(cls, canonical_name: str, version: int) -> tuple:
Expand Down
14 changes: 12 additions & 2 deletions packages/syft/src/syft/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
from nacl.signing import VerifyKey
import nh3
import requests
from result import Err
from result import Ok

# relative
from ..serde.serialize import _serialize as serialize
Expand Down Expand Up @@ -158,8 +160,16 @@ def sizeof(o: Any) -> int:
return sizeof(data) / (1024.0 * 1024.0)


def get_mb_serialized_size(data: Any) -> float:
return sys.getsizeof(serialize(data, to_bytes=True)) / (1024 * 1024)
def get_mb_serialized_size(data: Any) -> Ok[float] | Err[str]:
try:
serialized_data = serialize(data, to_bytes=True)
return Ok(sys.getsizeof(serialized_data) / (1024 * 1024))
except Exception as e:
data_type = type(data)
return Err(
f"Failed to serialize data of type '{data_type.__module__}.{data_type.__name__}'. "
f"Data type not supported. Detailed error: {e}"
)


def extract_name(klass: type) -> str:
Expand Down

0 comments on commit f315b98

Please sign in to comment.