Skip to content

Commit 0c45746

Browse files
author
Leandro Rosemberg
committed
Handles ImageUploadErrors in rfapi
1 parent 2fc45eb commit 0c45746

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

roboflow/adapters/rfapi.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class UploadError(RoboflowError):
1818
pass
1919

2020

21+
class ImageUploadError(RoboflowError):
22+
def __init__(self, message, status_code=None):
23+
self.message = message
24+
self.status_code = status_code
25+
self.retries = 0
26+
super().__init__(self.message)
27+
28+
2129
def get_workspace(api_key, workspace_url):
2230
url = f"{API_URL}/{workspace_url}?api_key={api_key}"
2331
response = requests.get(url)
@@ -78,24 +86,29 @@ def upload_image(
7886

7987
else:
8088
# Hosted image upload url
81-
8289
upload_url = _hosted_upload_url(api_key, project_url, image_path, split, coalesced_batch_name, tag_names)
90+
8391
# Get response
8492
response = requests.post(upload_url, timeout=(300, 300))
93+
8594
responsejson = None
8695
try:
8796
responsejson = response.json()
8897
except Exception:
8998
pass
99+
90100
if response.status_code != 200:
91101
if responsejson:
92-
raise UploadError(f"Bad response: {response.status_code}: {responsejson}")
102+
raise ImageUploadError(responsejson, status_code=response.status_code)
93103
else:
94-
raise UploadError(f"Bad response: {response}")
104+
raise ImageUploadError(response)
105+
95106
if not responsejson: # fail fast
96-
raise UploadError(f"upload image {image_path} 200 OK, unexpected response: {response}")
107+
raise ImageUploadError(response, status_code=response.status_code)
108+
97109
if not (responsejson.get("success") or responsejson.get("duplicate")):
98-
raise UploadError(f"Server rejected image: {responsejson}")
110+
raise ImageUploadError(responsejson)
111+
99112
return responsejson
100113

101114

roboflow/core/project.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import requests
1313

1414
from roboflow.adapters import rfapi
15+
from roboflow.adapters.rfapi import ImageUploadError
1516
from roboflow.config import API_URL, DEMO_KEYS
1617
from roboflow.core.exceptions import UploadAnnotationError, UploadImageError
1718
from roboflow.core.version import Version
@@ -496,7 +497,7 @@ def single_upload(
496497
if image_path:
497498
t0 = time.time()
498499
try:
499-
retry = Retry(num_retry_uploads, Exception)
500+
retry = Retry(num_retry_uploads, ImageUploadError)
500501
uploaded_image = retry(
501502
rfapi.upload_image,
502503
self.__api_key,
@@ -512,13 +513,9 @@ def single_upload(
512513
)
513514
image_id = uploaded_image["id"] # type: ignore[index]
514515
upload_retry_attempts = retry.retries
515-
except rfapi.UploadError as e:
516-
raise UploadImageError(
517-
f"Error uploading image: {self._parse_upload_error(e)}",
518-
retry_attempts=upload_retry_attempts,
519-
)
520-
except BaseException as e:
521-
uploaded_image = {"error": e}
516+
except rfapi.ImageUploadError as e:
517+
e.retries = upload_retry_attempts
518+
raise e
522519
finally:
523520
upload_time = time.time() - t0
524521

0 commit comments

Comments
 (0)