Skip to content

Commit 7994c48

Browse files
author
Val Brodsky
committed
Add custom error reporting to labeling service
1 parent 6e0f003 commit 7994c48

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def execute(
164164
files (dict): file arguments for request
165165
timeout (float): Max allowed time for query execution,
166166
in seconds.
167+
raise_return_resource_not_found: By default the client relies on the caller to raise the correct exception when a resource is not found.
168+
If this is set to True, the client will raise a ResourceNotFoundError exception automatically.
169+
This simplifies processing.
170+
We recommend to use it only of api returns a clear and well-formed error when a resource not found for a given query.
171+
error_handlers (dict): A dictionary mapping graphql error code to handler functions.
172+
Allows a caller to handle specific errors reporting in a custom way or produce more user-friendly readable messages.
173+
167174
Returns:
168175
dict, parsed JSON response.
169176
Raises:

libs/labelbox/src/labelbox/schema/labeling_service.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from datetime import datetime
22
from enum import Enum
3+
import json
34
from typing import Any
45
from typing_extensions import Annotated
56

6-
from labelbox.exceptions import ResourceNotFoundError
7+
from labelbox.exceptions import LabelboxError, ResourceNotFoundError
78

89
from labelbox.pydantic_compat import BaseModel, Field
910
from labelbox.utils import _CamelCaseMixin
@@ -87,12 +88,29 @@ def request(self) -> 'LabelingService':
8788
}
8889
"""
8990
result = self.client.execute(query_str, {"projectId": self.project_id},
90-
raise_return_resource_not_found=True)
91+
raise_return_resource_not_found=True,
92+
error_handlers={
93+
"INTERNAL_SERVER_ERROR":
94+
self._raise_readable_errors
95+
})
9196
success = result["validateAndRequestProjectBoostWorkforce"]["success"]
9297
if not success:
9398
raise Exception("Failed to start labeling service")
9499
return LabelingService.get(self.client, self.project_id)
95100

101+
def _raise_readable_errors(self, response):
102+
errors = response.json().get('errors', [])
103+
if errors:
104+
message = errors[0].get(
105+
'message', json.dumps([{
106+
"errorMessage": "Unknown error"
107+
}]))
108+
errors = json.loads(message)
109+
error_messages = [error['errorMessage'] for error in errors]
110+
else:
111+
error_messages = ["Uknown error"]
112+
raise LabelboxError(". ".join(error_messages))
113+
96114
@classmethod
97115
def getOrCreate(cls, client, project_id: Cuid) -> 'LabelingService':
98116
"""

libs/labelbox/tests/integration/test_labeling_service.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ def test_request_labeling_service_moe_project(
4242
project.upsert_instructions('tests/integration/media/sample_pdf.pdf')
4343

4444
labeling_service = project.get_labeling_service()
45-
with pytest.raises(
46-
LabelboxError,
47-
match=
48-
'[{"errorType":"PROJECT_MODEL_CONFIG","errorMessage":"Project model config is not completed"}]'
49-
):
45+
with pytest.raises(LabelboxError,
46+
match='Project model config is not completed'):
5047
labeling_service.request()
5148
project.add_model_config(model_config.uid)
5249
project.set_project_model_setup_complete()
@@ -64,5 +61,8 @@ def test_request_labeling_service_incomplete_requirements(ontology, project):
6461
): # No labeling service by default
6562
labeling_service.request()
6663
project.connect_ontology(ontology)
67-
with pytest.raises(LabelboxError):
64+
with pytest.raises(
65+
LabelboxError,
66+
match=
67+
"['Data is missing', 'Ontology instructions are not completed']"):
6868
labeling_service.request()

0 commit comments

Comments
 (0)