Skip to content

Commit ed78604

Browse files
committed
nicer API exception messages
1 parent 192c447 commit ed78604

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

stream_chat/exceptions.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ class StreamChannelException(Exception):
99
class StreamAPIException(Exception):
1010
def __init__(self, response):
1111
self.response = response
12+
self.json_response = False
13+
1214
try:
13-
parsed_response = json.loads(response.text)
14-
self.error_code = parsed_response.get("data", {}).get("code", "unknown")
15-
self.error_message = parsed_response.get("data", {}).get(
15+
parsed_response = response.json()
16+
self.error_code = parsed_response.get("code", "unknown")
17+
self.error_message = parsed_response.get(
1618
"message", "unknown"
1719
)
20+
self.json_response = True
1821
except JSONDecodeError:
19-
self.json_response = False
22+
pass
2023

21-
def __repr__(self):
24+
def __str__(self):
2225
if self.json_response:
23-
return f"StreamChat error code {self.error_code}: ${self.error_message}"
26+
return f"StreamChat error code {self.error_code}: {self.error_message}"
2427
else:
25-
return f"StreamChat error HTTP code: ${self.response.status_code}"
28+
return f"StreamChat error HTTP code: {self.response.status_code}"

0 commit comments

Comments
 (0)