Skip to content

Commit 232ce47

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

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-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: 7 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,9 @@ 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,
162165
) -> Dict[str, Any]:
163166
"""Executes a GraphQL query.
164167
@@ -167,6 +170,8 @@ def execute(
167170
variables (dict): Variables to pass to the query.
168171
raise_return_resource_not_found (bool): If True, raise a
169172
ResourceNotFoundError if the query returns None.
173+
error_handlers (dict): A dictionary mapping graphql error code to handler functions.
174+
Allows a caller to handle specific errors reporting in a custom way or produce more user-friendly readable messages
170175
171176
Returns:
172177
dict: The response from the server.
@@ -180,6 +185,7 @@ def execute(
180185
experimental=experimental,
181186
error_log_key=error_log_key,
182187
raise_return_resource_not_found=raise_return_resource_not_found,
188+
error_handlers=error_handlers,
183189
)
184190

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

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

Lines changed: 14 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,24 @@ def request(self) -> "LabelingService":
105106
query_str,
106107
{"projectId": self.project_id},
107108
raise_return_resource_not_found=True,
109+
error_handlers={"MALFORMED_REQUEST": self._raise_readable_errors},
108110
)
109111
success = result["validateAndRequestProjectBoostWorkforce"]["success"]
110112
if not success:
111113
raise Exception("Failed to start labeling service")
112114
return LabelingService.get(self.client, self.project_id)
113115

116+
def _raise_readable_errors(self, response):
117+
errors = response.json().get("errors", [])
118+
if errors:
119+
message = errors[0].get(
120+
"errors", json.dumps([{"error": "Unknown error"}])
121+
)
122+
error_messages = [error["error"] for error in message]
123+
else:
124+
error_messages = ["Uknown error"]
125+
raise LabelboxError(". ".join(error_messages))
126+
114127
@classmethod
115128
def getOrCreate(cls, client, project_id: Cuid) -> "LabelingService":
116129
"""

0 commit comments

Comments
 (0)