Skip to content

Commit dff7491

Browse files
author
Val Brodsky
committed
Add back support for readable errors for labeling service
1 parent d686acd commit dff7491

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

libs/labelbox/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies = [
1212
"tqdm>=4.66.2",
1313
"geojson>=3.1.0",
1414
"mypy==1.10.1",
15-
"lbox-clients==1.0.0",
15+
"lbox-clients==1.1.0",
1616
]
1717
readme = "README.md"
1818
requires-python = ">=3.8"

libs/labelbox/src/labelbox/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import urllib.parse
99
from collections import defaultdict
1010
from types import MappingProxyType
11-
from typing import Any, Dict, List, Optional, Union, overload
11+
from typing import Any, Callable, Dict, List, Optional, Union, overload
1212

1313
import lbox.exceptions
1414
import requests
@@ -159,6 +159,10 @@ def execute(
159159
experimental=False,
160160
error_log_key="message",
161161
raise_return_resource_not_found=False,
162+
error_handlers: Optional[
163+
Dict[str, Callable[[requests.models.Response], None]]
164+
] = None,
165+
162166
) -> Dict[str, Any]:
163167
"""Executes a GraphQL query.
164168
@@ -167,6 +171,8 @@ def execute(
167171
variables (dict): Variables to pass to the query.
168172
raise_return_resource_not_found (bool): If True, raise a
169173
ResourceNotFoundError if the query returns None.
174+
error_handlers (dict): A dictionary mapping graphql error code to handler functions.
175+
Allows a caller to handle specific errors reporting in a custom way or produce more user-friendly readable messages
170176
171177
Returns:
172178
dict: The response from the server.
@@ -180,6 +186,7 @@ def execute(
180186
experimental=experimental,
181187
error_log_key=error_log_key,
182188
raise_return_resource_not_found=raise_return_resource_not_found,
189+
error_handlers=error_handlers,
183190
)
184191

185192
def upload_file(self, path: str) -> str:

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import json
12
from datetime import datetime
23
from typing import Any
34

4-
from lbox.exceptions import ResourceNotFoundError
5+
from lbox.exceptions import LabelboxError, ResourceNotFoundError
56

67
from labelbox.schema.labeling_service_dashboard import LabelingServiceDashboard
78
from labelbox.schema.labeling_service_status import LabelingServiceStatus
@@ -105,12 +106,28 @@ def request(self) -> "LabelingService":
105106
query_str,
106107
{"projectId": self.project_id},
107108
raise_return_resource_not_found=True,
109+
error_handlers={
110+
"MALFORMED_REQUEST":
111+
self._raise_readable_errors
112+
}
108113
)
109114
success = result["validateAndRequestProjectBoostWorkforce"]["success"]
110115
if not success:
111116
raise Exception("Failed to start labeling service")
112117
return LabelingService.get(self.client, self.project_id)
113118

119+
def _raise_readable_errors(self, response):
120+
errors = response.json().get('errors', [])
121+
if errors:
122+
message = errors[0].get(
123+
'errors', json.dumps([{
124+
"error": "Unknown error"
125+
}]))
126+
error_messages = [error['error'] for error in message]
127+
else:
128+
error_messages = ["Uknown error"]
129+
raise LabelboxError(". ".join(error_messages))
130+
114131
@classmethod
115132
def getOrCreate(cls, client, project_id: Cuid) -> "LabelingService":
116133
"""

0 commit comments

Comments
 (0)