Skip to content

Commit b874dd3

Browse files
Merge pull request #353 from edenai/SD2-1406-add-error-code-handling-nyckel
SD2 1406 add error code handling nyckel
2 parents a2e0c7d + afdcce9 commit b874dd3

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

edenai_apis/apis/nyckel/nyckel_api.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ def image__automl_classification__upload_data_async__launch_job(
349349
if file_ is not None:
350350
file_.close()
351351
if response.status_code >= 400:
352-
self._raise_provider_exception(url, post_parameters, response)
352+
# self._raise_provider_exception(url, post_parameters, response)
353+
self.handle_provider_error(response)
353354
try:
354355
original_response = response.json()
355356
except Exception as exp:
@@ -377,7 +378,7 @@ def image__automl_classification__upload_data_async__launch_job(
377378
raise ProviderException("Could not upload image data", 400) from exp
378379
if webook_response.status_code >= 400:
379380
self.__image__automl_classification_delete_image(project_id, data["id"])
380-
raise ProviderException("Could not upload image data", 400) from exp
381+
raise ProviderException("Could not upload image data", 400)
381382
return AsyncLaunchJobResponseType(provider_job_id=job_id)
382383

383384
def image__automl_classification__upload_data_async__get_job_result(
@@ -538,3 +539,45 @@ def image__automl_classification__delete_project(
538539
deleted=True
539540
),
540541
)
542+
543+
def handle_provider_error(self, response: requests.Response):
544+
"""
545+
402 Billing issue. Mostly likely because you have exceeded the free tier quota.
546+
403 Forbidden. Check your credentials.
547+
409 Resouce conflict. Commonly raised when trying to create a sample that already exists in the function (Nyckel does not allow duplicate samples). When annotating an existing sample, use the PUT samples endpoint instead.
548+
429 Throttled. You have exceeded either 25 requests per second or 25 concurrent requests.
549+
500 Internal error. Retry -- ideally with exponential backoff.
550+
503 Service temporarily unavailable. Retry -- ideally with exponential backoff.
551+
"""
552+
if response.status_code == 402:
553+
raise ProviderException(
554+
"Billing issue. Mostly likely because you have exceeded the free tier quota.",
555+
response.status_code,
556+
)
557+
elif response.status_code == 403:
558+
raise ProviderException("Forbidden. Check your credentials.")
559+
elif response.status_code == 409:
560+
raise ProviderException(
561+
"Resouce conflict. Commonly raised when trying to create a sample that already exists in the function (Nyckel does not allow duplicate samples). When annotating an existing sample, use the PUT samples endpoint instead.",
562+
response.status_code,
563+
)
564+
elif response.status_code == 429:
565+
raise ProviderException(
566+
"Throttled. You have exceeded either 25 requests per second or 25 concurrent requests.",
567+
response.status_code,
568+
)
569+
elif response.status_code == 500:
570+
raise ProviderException(
571+
"Internal error. Retry -- ideally with exponential backoff.",
572+
response.status_code,
573+
)
574+
elif response.status_code == 503:
575+
raise ProviderException(
576+
"Service temporarily unavailable. Retry -- ideally with exponential backoff.",
577+
response.status_code,
578+
)
579+
else:
580+
raise ProviderException(
581+
f"Unexpected error with status code {response.status_code}: {response.text}",
582+
response.status_code,
583+
)

edenai_apis/apis/nyckel/nyckel_helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ def check_webhook_result(job_id: str, webhook_settings: dict) -> Dict:
1616
"""
1717
webhook_token = webhook_settings["webhook_token"]
1818
api_key = webhook_settings["webhook_api_key"]
19+
query = f'content:"{job_id}"'
1920
webhook_get_url = (
2021
f"https://webhook.site/token/{webhook_token}/requests"
21-
+ f"?sorting=newest&query={urllib.parse.quote_plus('content:'+str(job_id))}"
22+
+ f"?sorting=newest&query={urllib.parse.quote_plus(query)}"
2223
)
2324
webhook_response = requests.get(url=webhook_get_url, headers={"Api-Key": api_key})
2425
response_status = webhook_response.status_code

0 commit comments

Comments
 (0)