Skip to content

Commit e684799

Browse files
author
Val Brodsky
committed
Fix exception import
1 parent 5f3c9cb commit e684799

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
import requests
1515
import requests.exceptions
1616
from google.api_core import retry
17+
from lbox.exceptions import (
18+
InternalServerError,
19+
LabelboxError,
20+
ResourceNotFoundError,
21+
TimeoutError,
22+
)
1723
from lbox.request_client import RequestClient
1824

1925
from labelbox import __version__ as SDK_VERSION
@@ -111,7 +117,7 @@ def __init__(
111117
enable_experimental (bool): Indicates whether or not to use experimental features
112118
app_url (str) : host url for all links to the web app
113119
Raises:
114-
lbox.exceptions.AuthenticationError: If no `api_key`
120+
AuthenticationError: If no `api_key`
115121
is provided as an argument or via the environment
116122
variable.
117123
"""
@@ -199,7 +205,7 @@ def upload_file(self, path: str) -> str:
199205
Returns:
200206
str, the URL of uploaded data.
201207
Raises:
202-
lbox.exceptions.LabelboxError: If upload failed.
208+
LabelboxError: If upload failed.
203209
"""
204210
content_type, _ = mimetypes.guess_type(path)
205211
filename = os.path.basename(path)
@@ -209,7 +215,7 @@ def upload_file(self, path: str) -> str:
209215
)
210216

211217
@retry.Retry(
212-
predicate=retry.if_exception_type(lbox.exceptions.InternalServerError)
218+
predicate=retry.if_exception_type(InternalServerError)
213219
)
214220
def upload_data(
215221
self,
@@ -230,7 +236,7 @@ def upload_data(
230236
str, the URL of uploaded data.
231237
232238
Raises:
233-
lbox.exceptions.LabelboxError: If upload failed.
239+
LabelboxError: If upload failed.
234240
"""
235241

236242
request_data = {
@@ -271,16 +277,16 @@ def upload_data(
271277

272278
if response.status_code == 502:
273279
error_502 = "502 Bad Gateway"
274-
raise lbox.exceptions.InternalServerError(error_502)
280+
raise InternalServerError(error_502)
275281
elif response.status_code == 503:
276-
raise lbox.exceptions.InternalServerError(response.text)
282+
raise InternalServerError(response.text)
277283
elif response.status_code == 520:
278-
raise lbox.exceptions.InternalServerError(response.text)
284+
raise InternalServerError(response.text)
279285

280286
try:
281287
file_data = response.json().get("data", None)
282288
except ValueError as e: # response is not valid JSON
283-
raise lbox.exceptions.LabelboxError(
289+
raise LabelboxError(
284290
"Failed to upload, unknown cause", e
285291
)
286292

@@ -292,7 +298,7 @@ def upload_data(
292298
)
293299
except Exception:
294300
error_msg = "Unknown error"
295-
raise lbox.exceptions.LabelboxError(
301+
raise LabelboxError(
296302
"Failed to upload, message: %s" % error_msg
297303
)
298304

@@ -307,15 +313,15 @@ def _get_single(self, db_object_type, uid):
307313
Returns:
308314
Object of `db_object_type`.
309315
Raises:
310-
lbox.exceptions.ResourceNotFoundError: If there is no object
316+
ResourceNotFoundError: If there is no object
311317
of the given type for the given ID.
312318
"""
313319
query_str, params = query.get_single(db_object_type, uid)
314320

315321
res = self.execute(query_str, params)
316322
res = res and res.get(utils.camel_case(db_object_type.type_name()))
317323
if res is None:
318-
raise lbox.exceptions.ResourceNotFoundError(db_object_type, params)
324+
raise ResourceNotFoundError(db_object_type, params)
319325
else:
320326
return db_object_type(self, res)
321327

@@ -329,7 +335,7 @@ def get_project(self, project_id) -> Project:
329335
Returns:
330336
The sought Project.
331337
Raises:
332-
lbox.exceptions.ResourceNotFoundError: If there is no
338+
ResourceNotFoundError: If there is no
333339
Project with the given ID.
334340
"""
335341
return self._get_single(Entity.Project, project_id)
@@ -344,7 +350,7 @@ def get_dataset(self, dataset_id) -> Dataset:
344350
Returns:
345351
The sought Dataset.
346352
Raises:
347-
lbox.exceptions.ResourceNotFoundError: If there is no
353+
ResourceNotFoundError: If there is no
348354
Dataset with the given ID.
349355
"""
350356
return self._get_single(Entity.Dataset, dataset_id)
@@ -470,7 +476,7 @@ def _create(self, db_object_type, data, extra_params={}):
470476
)
471477

472478
if not res:
473-
raise lbox.exceptions.LabelboxError(
479+
raise LabelboxError(
474480
"Failed to create %s" % db_object_type.type_name()
475481
)
476482
res = res["create%s" % db_object_type.type_name()]
@@ -528,7 +534,7 @@ def delete_model_config(self, id: str) -> bool:
528534
params = {"id": id}
529535
result = self.execute(query, params)
530536
if not result:
531-
raise lbox.exceptions.ResourceNotFoundError(
537+
raise ResourceNotFoundError(
532538
Entity.ModelConfig, params
533539
)
534540
return result["deleteModelConfig"]["success"]
@@ -589,7 +595,7 @@ def create_dataset(
589595
)
590596

591597
if not validation_result["validateDataset"]["valid"]:
592-
raise lbox.exceptions.LabelboxError(
598+
raise LabelboxError(
593599
"IAMIntegration was not successfully added to the dataset."
594600
)
595601
except Exception as e:
@@ -895,7 +901,7 @@ def get_data_row_by_global_key(self, global_key: str) -> DataRow:
895901
"""
896902
res = self.get_data_row_ids_for_global_keys([global_key])
897903
if res["status"] != "SUCCESS":
898-
raise lbox.exceptions.ResourceNotFoundError(
904+
raise ResourceNotFoundError(
899905
Entity.DataRow, {global_key: global_key}
900906
)
901907
data_row_id = res["results"][0]
@@ -923,7 +929,7 @@ def get_model(self, model_id) -> Model:
923929
Returns:
924930
The sought Model.
925931
Raises:
926-
lbox.exceptions.ResourceNotFoundError: If there is no
932+
ResourceNotFoundError: If there is no
927933
Model with the given ID.
928934
"""
929935
return self._get_single(Entity.Model, model_id)
@@ -1169,7 +1175,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
11691175
response = self.connection.delete(endpoint)
11701176

11711177
if response.status_code != requests.codes.no_content:
1172-
raise lbox.exceptions.LabelboxError(
1178+
raise LabelboxError(
11731179
"Failed to delete the feature schema, message: "
11741180
+ str(response.json()["message"])
11751181
)
@@ -1190,7 +1196,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
11901196
response = self.connection.delete(endpoint)
11911197

11921198
if response.status_code != requests.codes.no_content:
1193-
raise lbox.exceptions.LabelboxError(
1199+
raise LabelboxError(
11941200
"Failed to delete the ontology, message: "
11951201
+ str(response.json()["message"])
11961202
)
@@ -1220,7 +1226,7 @@ def update_feature_schema_title(
12201226
if response.status_code == requests.codes.ok:
12211227
return self.get_feature_schema(feature_schema_id)
12221228
else:
1223-
raise lbox.exceptions.LabelboxError(
1229+
raise LabelboxError(
12241230
"Failed to update the feature schema, message: "
12251231
+ str(response.json()["message"])
12261232
)
@@ -1256,7 +1262,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
12561262
if response.status_code == requests.codes.ok:
12571263
return self.get_feature_schema(response.json()["schemaId"])
12581264
else:
1259-
raise lbox.exceptions.LabelboxError(
1265+
raise LabelboxError(
12601266
"Failed to upsert the feature schema, message: "
12611267
+ str(response.json()["message"])
12621268
)
@@ -1284,7 +1290,7 @@ def insert_feature_schema_into_ontology(
12841290
)
12851291
response = self.connection.post(endpoint, json={"position": position})
12861292
if response.status_code != requests.codes.created:
1287-
raise lbox.exceptions.LabelboxError(
1293+
raise LabelboxError(
12881294
"Failed to insert the feature schema into the ontology, message: "
12891295
+ str(response.json()["message"])
12901296
)
@@ -1309,7 +1315,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
13091315
if response.status_code == requests.codes.ok:
13101316
return response.json()
13111317
else:
1312-
raise lbox.exceptions.LabelboxError(
1318+
raise LabelboxError(
13131319
"Failed to get unused ontologies, message: "
13141320
+ str(response.json()["message"])
13151321
)
@@ -1334,7 +1340,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
13341340
if response.status_code == requests.codes.ok:
13351341
return response.json()
13361342
else:
1337-
raise lbox.exceptions.LabelboxError(
1343+
raise LabelboxError(
13381344
"Failed to get unused feature schemas, message: "
13391345
+ str(response.json()["message"])
13401346
)
@@ -1630,12 +1636,12 @@ def _format_failed_rows(
16301636
elif (
16311637
res["assignGlobalKeysToDataRowsResult"]["jobStatus"] == "FAILED"
16321638
):
1633-
raise lbox.exceptions.LabelboxError(
1639+
raise LabelboxError(
16341640
"Job assign_global_keys_to_data_rows failed."
16351641
)
16361642
current_time = time.time()
16371643
if current_time - start_time > timeout_seconds:
1638-
raise lbox.exceptions.TimeoutError(
1644+
raise TimeoutError(
16391645
"Timed out waiting for assign_global_keys_to_data_rows job to complete."
16401646
)
16411647
time.sleep(sleep_time)
@@ -1739,12 +1745,12 @@ def _format_failed_rows(
17391745

17401746
return {"status": status, "results": results, "errors": errors}
17411747
elif res["dataRowsForGlobalKeysResult"]["jobStatus"] == "FAILED":
1742-
raise lbox.exceptions.LabelboxError(
1748+
raise LabelboxError(
17431749
"Job dataRowsForGlobalKeys failed."
17441750
)
17451751
current_time = time.time()
17461752
if current_time - start_time > timeout_seconds:
1747-
raise lbox.exceptions.TimeoutError(
1753+
raise TimeoutError(
17481754
"Timed out waiting for get_data_rows_for_global_keys job to complete."
17491755
)
17501756
time.sleep(sleep_time)
@@ -1843,12 +1849,12 @@ def _format_failed_rows(
18431849

18441850
return {"status": status, "results": results, "errors": errors}
18451851
elif res["clearGlobalKeysResult"]["jobStatus"] == "FAILED":
1846-
raise lbox.exceptions.LabelboxError(
1852+
raise LabelboxError(
18471853
"Job clearGlobalKeys failed."
18481854
)
18491855
current_time = time.time()
18501856
if current_time - start_time > timeout_seconds:
1851-
raise lbox.exceptions.TimeoutError(
1857+
raise TimeoutError(
18521858
"Timed out waiting for clear_global_keys job to complete."
18531859
)
18541860
time.sleep(sleep_time)
@@ -1913,14 +1919,14 @@ def is_feature_schema_archived(
19131919
if filtered_feature_schema_nodes:
19141920
return bool(filtered_feature_schema_nodes[0]["archived"])
19151921
else:
1916-
raise lbox.exceptions.LabelboxError(
1922+
raise LabelboxError(
19171923
"The specified feature schema was not in the ontology."
19181924
)
19191925

19201926
elif response.status_code == 404:
1921-
raise lbox.exceptions.ResourceNotFoundError(Ontology, ontology_id)
1927+
raise ResourceNotFoundError(Ontology, ontology_id)
19221928
else:
1923-
raise lbox.exceptions.LabelboxError(
1929+
raise LabelboxError(
19241930
"Failed to get the feature schema archived status."
19251931
)
19261932

@@ -1947,7 +1953,7 @@ def get_model_slice(self, slice_id) -> ModelSlice:
19471953
"""
19481954
res = self.execute(query_str, {"id": slice_id})
19491955
if res is None or res["getSavedQuery"] is None:
1950-
raise lbox.exceptions.ResourceNotFoundError(ModelSlice, slice_id)
1956+
raise ResourceNotFoundError(ModelSlice, slice_id)
19511957

19521958
return Entity.ModelSlice(self, res["getSavedQuery"])
19531959

@@ -1994,7 +2000,7 @@ def delete_feature_schema_from_ontology(
19942000
result.deleted = bool(response_json["deleted"])
19952001
return result
19962002
else:
1997-
raise lbox.exceptions.LabelboxError(
2003+
raise LabelboxError(
19982004
"Failed to remove feature schema from ontology, message: "
19992005
+ str(response.json()["message"])
20002006
)
@@ -2022,11 +2028,11 @@ def unarchive_feature_schema_node(
20222028
response = self.connection.patch(ontology_endpoint)
20232029
if response.status_code == requests.codes.ok:
20242030
if not bool(response.json()["unarchived"]):
2025-
raise lbox.exceptions.LabelboxError(
2031+
raise LabelboxError(
20262032
"Failed unarchive the feature schema."
20272033
)
20282034
else:
2029-
raise lbox.exceptions.LabelboxError(
2035+
raise LabelboxError(
20302036
"Failed unarchive the feature schema node, message: ",
20312037
response.text,
20322038
)
@@ -2255,7 +2261,7 @@ def get_embedding_by_name(self, name: str) -> Embedding:
22552261
for e in embeddings:
22562262
if e.name == name:
22572263
return e
2258-
raise lbox.exceptions.ResourceNotFoundError(Embedding, dict(name=name))
2264+
raise ResourceNotFoundError(Embedding, dict(name=name))
22592265

22602266
def upsert_label_feedback(
22612267
self, label_id: str, feedback: str, scores: Dict[str, float]
@@ -2378,7 +2384,7 @@ def get_task_by_id(self, task_id: str) -> Union[Task, DataUpsertTask]:
23782384
result = self.execute(query, {"userId": user.uid, "taskId": task_id})
23792385
data = result.get("user", {}).get("createdTasks", [])
23802386
if not data:
2381-
raise lbox.exceptions.ResourceNotFoundError(
2387+
raise ResourceNotFoundError(
23822388
message=f"The task {task_id} does not exist."
23832389
)
23842390
task_data = data[0]

0 commit comments

Comments
 (0)