Skip to content

Commit 3fd2ac4

Browse files
author
Leandro Rosemberg
committed
Handles AnnotaitonSaveError in rfapi
1 parent 0c45746 commit 3fd2ac4

File tree

2 files changed

+18
-54
lines changed

2 files changed

+18
-54
lines changed

roboflow/adapters/rfapi.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ class ImageUploadError(RoboflowError):
2222
def __init__(self, message, status_code=None):
2323
self.message = message
2424
self.status_code = status_code
25-
self.retries = 0
25+
super().__init__(self.message)
26+
27+
28+
class AnnotationSaveError(RoboflowError):
29+
def __init__(self, message, status_code=None):
30+
self.message = message
31+
self.status_code = status_code
2632
super().__init__(self.message)
2733

2834

@@ -101,7 +107,7 @@ def upload_image(
101107
if responsejson:
102108
raise ImageUploadError(responsejson, status_code=response.status_code)
103109
else:
104-
raise ImageUploadError(response)
110+
raise ImageUploadError(response, status_code=response.status_code)
105111

106112
if not responsejson: # fail fast
107113
raise ImageUploadError(response, status_code=response.status_code)
@@ -141,24 +147,27 @@ def save_annotation(
141147
headers={"Content-Type": "application/json"},
142148
timeout=(60, 60),
143149
)
150+
151+
# Handle response
144152
responsejson = None
145153
try:
146154
responsejson = response.json()
147155
except Exception:
148156
pass
157+
149158
if not responsejson:
150-
raise _save_annotation_error(image_id, response)
151-
if response.status_code not in (200, 409):
152-
raise _save_annotation_error(image_id, response)
159+
raise AnnotationSaveError(response, status_code=response.status_code)
160+
153161
if response.status_code == 409:
154162
if "already annotated" in responsejson.get("error", {}).get("message"):
155163
return {"warn": "already annotated"}
156-
else:
157-
raise _save_annotation_error(image_id, response)
164+
158165
if responsejson.get("error"):
159-
raise _save_annotation_error(image_id, response)
166+
raise AnnotationSaveError(responsejson['error'], status_code=response.status_code)
167+
160168
if not responsejson.get("success"):
161-
raise _save_annotation_error(image_id, response)
169+
raise AnnotationSaveError(responsejson, status_code=response.status_code)
170+
162171
return responsejson
163172

164173

@@ -202,19 +211,3 @@ def _local_upload_url(api_key, project_url, batch_name, tag_names, sequence_numb
202211
query_params.update(sequence_number=sequence_number, sequence_size=sequence_size)
203212

204213
return _upload_url(api_key, project_url, **query_params)
205-
206-
207-
def _save_annotation_error(image_id, response):
208-
errmsg = f"save annotation for {image_id} / "
209-
responsejson = None
210-
try:
211-
responsejson = response.json()
212-
except Exception:
213-
pass
214-
if not responsejson:
215-
errmsg += f"bad response: {response.status_code}: {response}"
216-
elif responsejson.get("error"):
217-
errmsg += f"bad response: {response.status_code}: {responsejson['error']}"
218-
else:
219-
errmsg += f"bad response: {response.status_code}: {responsejson}"
220-
return UploadError(errmsg)

roboflow/core/project.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,6 @@ def single_upload(
513513
)
514514
image_id = uploaded_image["id"] # type: ignore[index]
515515
upload_retry_attempts = retry.retries
516-
except rfapi.ImageUploadError as e:
517-
e.retries = upload_retry_attempts
518-
raise e
519516
finally:
520517
upload_time = time.time() - t0
521518

@@ -535,15 +532,6 @@ def single_upload(
535532
annotation_labelmap=annotation_labelmap,
536533
overwrite=annotation_overwrite,
537534
)
538-
except rfapi.UploadError as e:
539-
raise UploadAnnotationError(
540-
f"Error uploading annotation: {self._parse_upload_error(e)}",
541-
image_id=image_id,
542-
image_upload_time=upload_time,
543-
image_retry_attempts=upload_retry_attempts,
544-
)
545-
except BaseException as e:
546-
uploaded_annotation = {"error": e}
547535
finally:
548536
annotation_time = time.time() - t0
549537
return {
@@ -574,23 +562,6 @@ def _annotation_params(self, annotation_path):
574562
)
575563
return annotation_name, annotation_string
576564

577-
def _parse_upload_error(self, error: rfapi.UploadError) -> str:
578-
error_str = str(error)
579-
start_idx = error_str.index("{")
580-
end_idx = error_str.rindex("}") + 1
581-
dict_part = error_str[start_idx:end_idx]
582-
dict_part = dict_part.replace("True", "true")
583-
dict_part = dict_part.replace("False", "false")
584-
dict_part = dict_part.replace("None", "null")
585-
if re.search(r"'\w+':", dict_part):
586-
temp_str = dict_part.replace(r"\'", "<PLACEHOLDER>")
587-
temp_str = temp_str.replace('"', r"\"")
588-
temp_str = temp_str.replace("'", '"')
589-
dict_part = temp_str.replace("<PLACEHOLDER>", "'")
590-
parsed_dict: dict = json.loads(dict_part)
591-
message = parsed_dict.get("message")
592-
return message or str(parsed_dict)
593-
594565
def search(
595566
self,
596567
like_image: Optional[str] = None,

0 commit comments

Comments
 (0)