Skip to content

Commit 3300f77

Browse files
rllinrllin
authored andcommitted
retry
1 parent 96e1c76 commit 3300f77

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

labelbox/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from typing import Tuple
77

8+
from google.api_core import retry
89
import requests
910
import requests.exceptions
1011

@@ -60,6 +61,8 @@ def __init__(self,
6061
'Authorization': 'Bearer %s' % api_key
6162
}
6263

64+
@retry.Retry(predicate=retry.if_exception_type(
65+
labelbox.exceptions.InternalServerError))
6366
def execute(self, query, params=None, timeout=10.0):
6467
""" Sends a request to the server for the execution of the
6568
given query. Checks the response for errors and wraps errors
@@ -123,6 +126,9 @@ def convert_value(value):
123126
try:
124127
r_json = response.json()
125128
except:
129+
error_502 = '502 Bad Gateway'
130+
if error_502 in response.text:
131+
raise labelbox.exceptions.InternalServerError(error_502)
126132
raise labelbox.exceptions.LabelboxError(
127133
"Failed to parse response as JSON: %s" % response.text)
128134

@@ -170,6 +176,12 @@ def check_errors(keywords, *path):
170176
if response_msg.startswith("You have exceeded"):
171177
raise labelbox.exceptions.ApiLimitError(response_msg)
172178

179+
prisma_error = check_errors(["INTERNAL_SERVER_ERROR"], "extensions",
180+
"code")
181+
if prisma_error:
182+
raise labelbox.exceptions.InternalServerError(
183+
prisma_error["message"])
184+
173185
if len(errors) > 0:
174186
logger.warning("Unparsed errors on query execution: %r", errors)
175187
raise labelbox.exceptions.LabelboxError("Unknown error: %s" %

labelbox/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class ValidationFailedError(LabelboxError):
4848
pass
4949

5050

51+
class InternalServerError(LabelboxError):
52+
"""Nondescript prisma or 502 related errors.
53+
54+
Meant to be retryable.
55+
56+
TODO: these errors need better messages from platform
57+
"""
58+
pass
59+
60+
5161
class InvalidQueryError(LabelboxError):
5262
""" Indicates a malconstructed or unsupported query (either by GraphQL in
5363
general or by Labelbox specifically). This can be the result of either client

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"backoff==1.10.0",
1818
"ndjson==0.3.1",
1919
"requests>=2.22.0",
20+
"google-api-core>=1.22.1",
2021
],
2122
classifiers=[
2223
'Development Status :: 3 - Alpha',

0 commit comments

Comments
 (0)