Skip to content

Commit 6389f14

Browse files
authored
[PLT-1466] Vb/fix labeling service error reporting plt 1466 2 (#1836)
1 parent 2beab37 commit 6389f14

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
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
"""

libs/lbox-clients/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "lbox-clients"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
description = "This module contains client sdk uses to conntect to the Labelbox API and backends"
55
authors = [
66
{ name = "Labelbox", email = "engineering@labelbox.com" }

libs/lbox-clients/src/lbox/request_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,13 @@ def get_error_status_code(error: dict) -> int:
304304
malformed_request_error = check_errors(
305305
["MALFORMED_REQUEST"], "extensions", "code"
306306
)
307+
308+
error_code = "MALFORMED_REQUEST"
307309
if malformed_request_error is not None:
310+
if error_handlers and error_code in error_handlers:
311+
handler = error_handlers[error_code]
312+
handler(response)
313+
return None
308314
raise exceptions.MalformedQueryException(
309315
malformed_request_error[error_log_key]
310316
)

0 commit comments

Comments
 (0)