|
4 | 4 | from typing import Optional |
5 | 5 |
|
6 | 6 | import requests |
| 7 | +from requests.exceptions import RequestException |
7 | 8 | from requests_toolbelt.multipart.encoder import MultipartEncoder |
8 | 9 |
|
9 | 10 | from roboflow.config import API_URL, DEFAULT_BATCH_NAME, DEFAULT_JOB_NAME |
@@ -85,14 +86,21 @@ def upload_image( |
85 | 86 | "file": ("imageToUpload", imgjpeg, "image/jpeg"), |
86 | 87 | } |
87 | 88 | ) |
88 | | - response = requests.post(upload_url, data=m, headers={"Content-Type": m.content_type}, timeout=(300, 300)) |
| 89 | + |
| 90 | + try: |
| 91 | + response = requests.post(upload_url, data=m, headers={"Content-Type": m.content_type}, timeout=(300, 300)) |
| 92 | + except RequestException as e: |
| 93 | + raise ImageUploadError(str(e)) from e |
89 | 94 |
|
90 | 95 | else: |
91 | 96 | # Hosted image upload url |
92 | 97 | upload_url = _hosted_upload_url(api_key, project_url, image_path, split, coalesced_batch_name, tag_names) |
93 | 98 |
|
94 | | - # Get response |
95 | | - response = requests.post(upload_url, timeout=(300, 300)) |
| 99 | + try: |
| 100 | + # Get response |
| 101 | + response = requests.post(upload_url, timeout=(300, 300)) |
| 102 | + except RequestException as e: |
| 103 | + raise ImageUploadError(str(e)) from e |
96 | 104 |
|
97 | 105 | responsejson = None |
98 | 106 | try: |
@@ -147,12 +155,15 @@ def save_annotation( |
147 | 155 | api_key, project_url, annotation_name, image_id, job_name, is_prediction, overwrite |
148 | 156 | ) |
149 | 157 |
|
150 | | - response = requests.post( |
151 | | - upload_url, |
152 | | - data=json.dumps({"annotationFile": annotation_string, "labelmap": annotation_labelmap}), |
153 | | - headers={"Content-Type": "application/json"}, |
154 | | - timeout=(60, 60), |
155 | | - ) |
| 158 | + try: |
| 159 | + response = requests.post( |
| 160 | + upload_url, |
| 161 | + data=json.dumps({"annotationFile": annotation_string, "labelmap": annotation_labelmap}), |
| 162 | + headers={"Content-Type": "application/json"}, |
| 163 | + timeout=(60, 60), |
| 164 | + ) |
| 165 | + except RequestException as e: |
| 166 | + raise AnnotationSaveError(str(e)) from e |
156 | 167 |
|
157 | 168 | # Handle response |
158 | 169 | responsejson = None |
|
0 commit comments