Skip to content

Commit 4fbae4d

Browse files
Improve Exception handling.
1 parent 7ddb20c commit 4fbae4d

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

labelbox/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def execute(self, query, params=None, timeout=10.0):
7575
labelbox.exceptions.InvalidQueryError: If `query` is not
7676
syntactically or semantically valid (checked server-side).
7777
labelbox.exceptions.ApiLimitError: If the server API limit was
78-
exceeded. See "How to import data" in the online documentation
78+
exceeded. See "How to import data" in the online documentation
7979
to see API limits.
8080
labelbox.exceptions.TimeoutError: If response was not received
8181
in `timeout` seconds.
@@ -112,14 +112,14 @@ def convert_value(value):
112112
raise labelbox.exceptions.NetworkError(e)
113113

114114
except Exception as e:
115-
logger.error("Unknown error: %s", str(e))
116-
raise labelbox.exceptions.LabelboxError(str(e))
115+
raise labelbox.exceptions.LabelboxError(
116+
"Unknown error during Client.query(): " + str(e), e)
117117

118118
try:
119119
response = response.json()
120120
except:
121121
raise labelbox.exceptions.LabelboxError(
122-
"Failed to parse response as JSON: %s", response.text)
122+
"Failed to parse response as JSON: %s" % response.text)
123123

124124
errors = response.get("errors", [])
125125

@@ -173,7 +173,7 @@ def check_errors(keywords, *path):
173173

174174
def upload_data(self, data):
175175
""" Uploads the given data (bytes) to Labelbox.
176-
176+
177177
Args:
178178
data (bytes): The data to upload.
179179
Returns:
@@ -199,9 +199,9 @@ def upload_data(self, data):
199199

200200
try:
201201
file_data = response.json().get("data", None)
202-
except ValueError: # response is not valid JSON
202+
except ValueError as e: # response is not valid JSON
203203
raise labelbox.exceptions.LabelboxError(
204-
"Failed to upload, unknown cause")
204+
"Failed to upload, unknown cause", e)
205205

206206
if not file_data or not file_data.get("uploadFile", None):
207207
raise labelbox.exceptions.LabelboxError(

labelbox/exceptions.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
class LabelboxError(Exception):
22
"""Base class for exceptions."""
3-
def __init__(self, message, *args):
4-
super().__init__(*args)
3+
def __init__(self, message, cause=None):
4+
"""
5+
Args:
6+
message (str): Informative message about the exception.
7+
cause (Exception): The cause of the exception (an Exception
8+
raised by Python or another library). Optional.
9+
"""
10+
super().__init__(message, cause)
511
self.message = message
12+
self.cause = cause
13+
14+
def __str__(self):
15+
return self._message + str(self.args)
616

717

818
class AuthenticationError(LabelboxError):
@@ -31,9 +41,8 @@ def __init__(self, db_object_type, params):
3141

3242

3343
class ValidationFailedError(LabelboxError):
34-
"""Exception raised for when a GraphQL query fails validation (query cost, etc.)
35-
36-
E.g. a query that is too expensive, or depth is too deep.
44+
"""Exception raised for when a GraphQL query fails validation (query cost,
45+
etc.) E.g. a query that is too expensive, or depth is too deep.
3746
"""
3847
pass
3948

@@ -47,10 +56,8 @@ class InvalidQueryError(LabelboxError):
4756

4857
class NetworkError(LabelboxError):
4958
"""Raised when an HTTPError occurs."""
50-
def __init__(self, cause, message=None):
51-
if message is None:
52-
message = str(cause)
53-
super().__init__(message)
59+
def __init__(self, cause):
60+
super().__init__(str(cause), cause)
5461
self.cause = cause
5562

5663

0 commit comments

Comments
 (0)