Skip to content

Commit 5dc1fed

Browse files
feat(lab-3842): give more details on appendToLabel errors
1 parent 9ae0190 commit 5dc1fed

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Helper functions to extract context from GraphQL error messages."""
2+
import ast
3+
from typing import Dict, Optional
4+
5+
6+
def extract_error_context(message: str) -> Optional[Dict[str, str]]:
7+
"""Parse the string error message to extract the first context information."""
8+
try:
9+
parsed_errors = ast.literal_eval(message)
10+
return parsed_errors[0].get("extensions").get("context")
11+
except (SyntaxError, ValueError):
12+
return None

src/kili/core/graphql/graphql_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from kili.adapters.http_client import HttpClient
3434
from kili.core.constants import MAX_CALLS_PER_MINUTE
3535
from kili.core.graphql.clientnames import GraphQLClientName
36+
from kili.core.graphql.exceptions import extract_error_context
3637
from kili.utils.logcontext import LogContext
3738

3839
gql_requests_logger.setLevel(logging.WARNING)
@@ -281,8 +282,8 @@ def execute(
281282
raise kili.exceptions.GraphQLError(error=err.errors) from err
282283

283284
except exceptions.TransportQueryError as err: # remove validation error
284-
# the server refused the query after some retries, we crash
285-
raise kili.exceptions.GraphQLError(error=err.errors) from err
285+
context = extract_error_context(str(err.errors))
286+
raise kili.exceptions.GraphQLError(error=err.errors, context=context) from err
286287

287288
@retry(
288289
reraise=True, # re-raise the last exception

src/kili/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
class GraphQLError(Exception):
77
"""Raised when the GraphQL call returns an error."""
88

9-
def __init__(self, error, batch_number=None) -> None:
9+
def __init__(self, error, batch_number=None, context=None) -> None:
1010
self.error = error
11+
self.context = context
1112

1213
if isinstance(error, List):
1314
error = error[0]

src/kili/use_cases/label/__init__.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from kili.domain.project import ProjectId
1515
from kili.domain.types import ListOrTuple
1616
from kili.domain.user import UserId
17+
from kili.exceptions import GraphQLError
1718
from kili.use_cases.asset.utils import AssetUseCasesUtils
1819
from kili.use_cases.base import BaseUseCases
1920
from kili.utils.labels.parsing import parse_labels
@@ -25,6 +26,23 @@
2526
import pandas as pd
2627

2728

29+
LOCK_ERRORS = {
30+
"AlreadyHaveLabelOnCurrentStep": "You cannot edit this asset as you've already submitted a label.",
31+
"AssetAlreadyAssigned": "This asset is assigned to someone else. You cannot edit it.",
32+
"AssetAlreadyLocked": "This asset is currently being edited by another user.",
33+
"AssetInDoneStepStatus": "This asset is already labeled or reviewed and cannot be edited.",
34+
"AssetInDoneStepStatusWithCorrectAction": "You already labeled this asset, but you can still correct it.",
35+
"AssetInDoneStepStatusWithReviewAction": "This asset is completed. Take it for review to make changes.",
36+
"AssetInNextStepWithCorrectAction": "This asset is awaiting to be reviewed but can still be corrected.",
37+
"AssetLockedNoReason": "This asset is currently locked. You cannot edit it.",
38+
"BlockedByEnforceStepSeparation": "You can't edit this asset as you already worked on another step.",
39+
"LabelNotEditable": "This label was created in another step and is read-only.",
40+
"NotAssignedWithAssignAction": "This asset is in read-only mode because you are not assigned to it."
41+
+ "To make edits, add yourself as an assignee.",
42+
"NotInStepAssignees": "You cannot edit this asset in its current step.",
43+
}
44+
45+
2846
class LabelUseCases(BaseUseCases):
2947
"""Label use cases."""
3048

@@ -116,12 +134,20 @@ def append_labels(
116134
overwrite=overwrite,
117135
labels_data=labels_to_add,
118136
)
119-
return self._kili_api_gateway.append_many_labels(
120-
fields=fields,
121-
disable_tqdm=disable_tqdm,
122-
data=data,
123-
project_id=project_id,
124-
)
137+
try:
138+
return self._kili_api_gateway.append_many_labels(
139+
fields=fields,
140+
disable_tqdm=disable_tqdm,
141+
data=data,
142+
project_id=project_id,
143+
)
144+
except GraphQLError as e:
145+
if e.context and e.context.get("reason"):
146+
reason = e.context.get("reason")
147+
if reason in LOCK_ERRORS:
148+
raise ValueError(LOCK_ERRORS[reason]) from e
149+
150+
raise e
125151

126152
def append_to_labels(
127153
self,

0 commit comments

Comments
 (0)