Skip to content

Commit 069c4e8

Browse files
committed
SCP-8 updated exceptions
1 parent a1e3c4f commit 069c4e8

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
class JobNotFoundException(Exception):
2+
def __init__(self, message):
3+
super().__init__(message)
4+
15
class StudioException(Exception):
26
def __init__(self, status_code, message="Studio exception occurred"):
37
self.status_code = status_code
48
self.message = message
59
super().__init__(self.message, self.status_code)
6-

skylab_studio/studio_client.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import sentry_sdk
1919

2020
from .version import VERSION
21-
from .studio_exception import StudioException
21+
from exceptions.exceptions import JobNotFoundException, StudioException
2222

2323
API_HEADER_KEY = 'X-SLT-API-KEY'
2424
API_HEADER_CLIENT = 'X-SLT-API-CLIENT'
@@ -83,6 +83,7 @@ def __init__(self, api_key=None, **kwargs):
8383
# of sampled transactions.
8484
# We recommend adjusting this value in production.
8585
profiles_sample_rate=1.0,
86+
ignore_errors=[JobNotFoundException]
8687
)
8788

8889
def _build_http_auth(self):
@@ -315,10 +316,14 @@ def _upload_photo(self, photo_path, id, model='job'):
315316
photo_data = { f"{model}_id": id, "name": photo_name, "use_cache_upload": False }
316317

317318
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}")
319326

320-
if job_type == 'regular':
321-
headers = { 'X-Amz-Tagging': 'job=photo&api=true' }
322327

323328
# Ask studio to create the photo record
324329
photo_resp = self._create_photo(photo_data)
@@ -352,20 +357,21 @@ def _upload_photo(self, photo_path, id, model='job'):
352357
retry = 0
353358
# retry upload
354359
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)
356366

357367
if upload_photo_resp:
358368
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
360370
raise Exception('Unable to upload to the bucket after retrying.')
361371
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))
363373
retry += 1
364374

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-
369375
except Exception as e:
370376
sentry_sdk.capture_exception(e)
371377
print(f"An exception of type {type(e).__name__} occurred: {e}")

0 commit comments

Comments
 (0)