Skip to content

Commit 9c473c8

Browse files
authored
[ML] Enable SDK data and datastore get calls to use new error message format (Azure#26788)
* enable SDK data and datastore get calls to use new error message format
1 parent 60ef9f0 commit 9c473c8

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

sdk/ml/azure-ai-ml/azure/ai/ml/operations/_data_operations.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,37 +121,39 @@ def get(self, name: str, version: Optional[str] = None, label: Optional[str] = N
121121
:return: Data asset object.
122122
:rtype: ~azure.ai.ml.entities.Data
123123
"""
124-
if version and label:
125-
msg = "Cannot specify both version and label."
126-
raise ValidationException(
127-
message=msg,
128-
target=ErrorTarget.DATA,
129-
no_personal_data_message=msg,
130-
error_category=ErrorCategory.USER_ERROR,
131-
error_type=ValidationErrorType.INVALID_VALUE,
132-
)
133-
134-
if label:
135-
return _resolve_label_to_asset(self, name, label)
124+
try:
125+
if version and label:
126+
msg = "Cannot specify both version and label."
127+
raise ValidationException(
128+
message=msg,
129+
target=ErrorTarget.DATA,
130+
no_personal_data_message=msg,
131+
error_category=ErrorCategory.USER_ERROR,
132+
error_type=ValidationErrorType.INVALID_VALUE,
133+
)
136134

137-
if not version:
138-
msg = "Must provide either version or label."
139-
raise ValidationException(
140-
message=msg,
141-
target=ErrorTarget.DATA,
142-
no_personal_data_message=msg,
143-
error_category=ErrorCategory.USER_ERROR,
144-
error_type=ValidationErrorType.MISSING_FIELD,
135+
if label:
136+
return _resolve_label_to_asset(self, name, label)
137+
138+
if not version:
139+
msg = "Must provide either version or label."
140+
raise ValidationException(
141+
message=msg,
142+
target=ErrorTarget.DATA,
143+
no_personal_data_message=msg,
144+
error_category=ErrorCategory.USER_ERROR,
145+
error_type=ValidationErrorType.MISSING_FIELD,
146+
)
147+
data_version_resource = self._operation.get(
148+
resource_group_name=self._resource_group_name,
149+
workspace_name=self._workspace_name,
150+
name=name,
151+
version=version,
152+
**self._init_kwargs,
145153
)
146-
data_version_resource = self._operation.get(
147-
resource_group_name=self._resource_group_name,
148-
workspace_name=self._workspace_name,
149-
name=name,
150-
version=version,
151-
**self._init_kwargs,
152-
)
153-
154-
return Data._from_rest_object(data_version_resource)
154+
return Data._from_rest_object(data_version_resource)
155+
except (ValidationException, SchemaValidationError) as ex:
156+
log_and_raise_error(ex)
155157

156158
# @monitor_with_activity(logger, "Data.CreateOrUpdate", ActivityType.PUBLICAPI)
157159
def create_or_update(self, data: Data) -> Data:

sdk/ml/azure-ai-ml/azure/ai/ml/operations/_datastore_operations.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,18 @@ def get(self, name: str, *, include_secrets: bool = False) -> Datastore:
101101
:return: Datastore with the specified name.
102102
:rtype: Datastore
103103
"""
104-
105-
datastore_resource = self._operation.get(
106-
name=name,
107-
resource_group_name=self._operation_scope.resource_group_name,
108-
workspace_name=self._workspace_name,
109-
**self._init_kwargs
110-
)
111-
if include_secrets:
112-
self._fetch_and_populate_secret(datastore_resource)
113-
return Datastore._from_rest_object(datastore_resource)
104+
try:
105+
datastore_resource = self._operation.get(
106+
name=name,
107+
resource_group_name=self._operation_scope.resource_group_name,
108+
workspace_name=self._workspace_name,
109+
**self._init_kwargs
110+
)
111+
if include_secrets:
112+
self._fetch_and_populate_secret(datastore_resource)
113+
return Datastore._from_rest_object(datastore_resource)
114+
except (ValidationException, SchemaValidationError) as ex:
115+
log_and_raise_error(ex)
114116

115117
def _fetch_and_populate_secret(self, datastore_resource: DatastoreData) -> None:
116118
if datastore_resource.name and not isinstance(
@@ -128,16 +130,18 @@ def get_default(self, *, include_secrets: bool = False) -> Datastore:
128130
:return: The default datastore.
129131
:rtype: Datastore
130132
"""
131-
132-
datastore_resource = self._operation.list(
133-
resource_group_name=self._operation_scope.resource_group_name,
134-
workspace_name=self._workspace_name,
135-
is_default=True,
136-
**self._init_kwargs
137-
).next()
138-
if include_secrets:
139-
self._fetch_and_populate_secret(datastore_resource)
140-
return Datastore._from_rest_object(datastore_resource)
133+
try:
134+
datastore_resource = self._operation.list(
135+
resource_group_name=self._operation_scope.resource_group_name,
136+
workspace_name=self._workspace_name,
137+
is_default=True,
138+
**self._init_kwargs
139+
).next()
140+
if include_secrets:
141+
self._fetch_and_populate_secret(datastore_resource)
142+
return Datastore._from_rest_object(datastore_resource)
143+
except (ValidationException, SchemaValidationError) as ex:
144+
log_and_raise_error(ex)
141145

142146
# @monitor_with_activity(logger, "Datastore.CreateOrUpdate", ActivityType.PUBLICAPI)
143147
def create_or_update(self, datastore: Datastore) -> Datastore:

sdk/ml/azure-ai-ml/tests/dataset/unittests/test_data_operations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ def test_get_with_version(self, mock_data_operations: DataOperations) -> None:
9191

9292
def test_get_no_version(self, mock_data_operations: DataOperations) -> None:
9393
name = "random_name"
94-
with pytest.raises(Exception):
94+
with pytest.raises(Exception) as ex:
9595
mock_data_operations.get(name=name)
96+
assert "At least one required parameter is missing" in str(ex.value)
9697

9798
def test_create_with_spec_file(
9899
self,

0 commit comments

Comments
 (0)