Skip to content

Commit 96e1c76

Browse files
rllinrllin
authored andcommitted
catchall for other errors
1 parent 4956bb5 commit 96e1c76

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

labelbox/client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,13 @@ def convert_value(value):
120120
raise labelbox.exceptions.LabelboxError(
121121
"Unknown error during Client.query(): " + str(e), e)
122122

123-
# if we do return a proper error code, reraise
124-
if response.status_code != requests.codes.ok:
125-
if response.status_code == 400:
126-
print('400', response)
127-
message = f"{response.status_code} {response.reason}"
128-
cause = None
129-
try:
130-
r_json = response.json()
131-
except:
132-
pass
133-
else:
134-
cause = r_json.get('message')
135-
raise labelbox.exceptions.LabelboxError(message, cause)
136-
137123
try:
138-
response = response.json()
124+
r_json = response.json()
139125
except:
140126
raise labelbox.exceptions.LabelboxError(
141127
"Failed to parse response as JSON: %s" % response.text)
142128

143-
errors = response.get("errors", [])
129+
errors = r_json.get("errors", [])
144130

145131
def check_errors(keywords, *path):
146132
""" Helper that looks for any of the given `keywords` in any of
@@ -180,7 +166,7 @@ def check_errors(keywords, *path):
180166
graphql_error["message"])
181167

182168
# Check if API limit was exceeded
183-
response_msg = response.get("message", "")
169+
response_msg = r_json.get("message", "")
184170
if response_msg.startswith("You have exceeded"):
185171
raise labelbox.exceptions.ApiLimitError(response_msg)
186172

@@ -189,7 +175,17 @@ def check_errors(keywords, *path):
189175
raise labelbox.exceptions.LabelboxError("Unknown error: %s" %
190176
str(errors))
191177

192-
return response["data"]
178+
# if we do return a proper error code, and didn't catch this above
179+
# reraise
180+
# this mainly catches a 401 for API access disabled for free tier
181+
# TODO: need to unify API errors to handle things more uniformly
182+
# in the SDK
183+
if response.status_code != requests.codes.ok:
184+
message = f"{response.status_code} {response.reason}"
185+
cause = r_json.get('message')
186+
raise labelbox.exceptions.LabelboxError(message, cause)
187+
188+
return r_json["data"]
193189

194190
def upload_file(self, path: str) -> str:
195191
"""Uploads given path to local file.

0 commit comments

Comments
 (0)