|
18 | 18 | import sentry_sdk |
19 | 19 |
|
20 | 20 | from .version import VERSION |
21 | | -from .studio_exception import StudioException |
| 21 | +from exceptions.exceptions import JobNotFoundException, StudioException |
22 | 22 |
|
23 | 23 | API_HEADER_KEY = 'X-SLT-API-KEY' |
24 | 24 | API_HEADER_CLIENT = 'X-SLT-API-CLIENT' |
@@ -83,6 +83,7 @@ def __init__(self, api_key=None, **kwargs): |
83 | 83 | # of sampled transactions. |
84 | 84 | # We recommend adjusting this value in production. |
85 | 85 | profiles_sample_rate=1.0, |
| 86 | + ignore_errors=[JobNotFoundException] |
86 | 87 | ) |
87 | 88 |
|
88 | 89 | def _build_http_auth(self): |
@@ -315,10 +316,14 @@ def _upload_photo(self, photo_path, id, model='job'): |
315 | 316 | photo_data = { f"{model}_id": id, "name": photo_name, "use_cache_upload": False } |
316 | 317 |
|
317 | 318 | if model == 'job': |
318 | | - job_type = self.get_job(id)['type'] |
| 319 | + job = self.get_job(id) |
| 320 | + if "type" in job: |
| 321 | + job_type = job['type'] |
| 322 | + if job_type == 'regular': |
| 323 | + headers = { 'X-Amz-Tagging': 'job=photo&api=true' } |
| 324 | + else: |
| 325 | + raise JobNotFoundException(f"Unable to find job with id: {id}") |
319 | 326 |
|
320 | | - if job_type == 'regular': |
321 | | - headers = { 'X-Amz-Tagging': 'job=photo&api=true' } |
322 | 327 |
|
323 | 328 | # Ask studio to create the photo record |
324 | 329 | photo_resp = self._create_photo(photo_data) |
@@ -352,20 +357,21 @@ def _upload_photo(self, photo_path, id, model='job'): |
352 | 357 | retry = 0 |
353 | 358 | # retry upload |
354 | 359 | while retry < 3: |
355 | | - upload_photo_resp = requests.put(upload_url, data, headers=headers) |
| 360 | + try: |
| 361 | + upload_photo_resp = requests.put(upload_url, data, headers=headers) |
| 362 | + |
| 363 | + except requests.exceptions.HTTPError as e: |
| 364 | + sentry_sdk.capture_exception(e) |
| 365 | + print("HTTP error occurred while trying to upload your photo:", e) |
356 | 366 |
|
357 | 367 | if upload_photo_resp: |
358 | 368 | break # Upload was successful, exit the loop |
359 | | - elif retry == 2: # Check if retry count is 2 (0-based indexing) |
| 369 | + elif retry == 2: # Check if it's the 3rd retry |
360 | 370 | raise Exception('Unable to upload to the bucket after retrying.') |
361 | 371 | else: |
362 | | - time.sleep(1) # Wait for a moment before retrying |
| 372 | + time.sleep(max(1, retry)) # Wait before retrying (number of retries in seconds (minimum 1)) |
363 | 373 | retry += 1 |
364 | 374 |
|
365 | | - except requests.exceptions.HTTPError as e: |
366 | | - sentry_sdk.capture_exception(e) |
367 | | - print("HTTP error occurred while trying to upload your photo:", e) |
368 | | - |
369 | 375 | except Exception as e: |
370 | 376 | sentry_sdk.capture_exception(e) |
371 | 377 | print(f"An exception of type {type(e).__name__} occurred: {e}") |
|
0 commit comments