Skip to content

Commit ad9d817

Browse files
committed
🛠️ fix CodeBoxError
1 parent c47cdae commit ad9d817

File tree

2 files changed

+14
-44
lines changed

2 files changed

+14
-44
lines changed

codeboxapi/errors.py

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
CodeBox API Error Classes
2+
CodeBox API
3+
Error Classes
34
"""
45

56

@@ -10,46 +11,19 @@ class CodeBoxError(Exception):
1011

1112
def __init__(
1213
self,
14+
http_status: int = 0,
15+
json_body: dict = {},
16+
headers: dict = {},
1317
**kwargs,
1418
):
1519
super().__init__(**kwargs)
1620

17-
if (http_body := kwargs.get("http_body")) and hasattr(http_body, "decode"):
18-
try:
19-
http_body = http_body.decode("utf-8")
20-
except BaseException:
21-
http_body = (
22-
"<Could not decode body as utf-8. "
23-
"Please report to [email protected]>"
24-
)
25-
26-
self._message = kwargs.get("message")
27-
self.http_body = http_body
28-
self.http_status = kwargs.get("http_status")
29-
self.json_body = kwargs.get("json_body")
30-
self.headers = kwargs.get("headers", {})
31-
self.code = kwargs.get("code", None)
32-
self.request_id = self.headers.get("request-id", None)
33-
# self.error = self.construct_error_object() # TODO: implement
34-
self.user_id = self.headers.get("user_id", None)
21+
self.http_status = http_status
22+
self.json_body = json_body
23+
self.headers = headers
3524

3625
def __str__(self):
37-
msg = self._message or "<empty message>"
38-
if self.request_id is not None:
39-
return f"Request {self.request_id}: {msg}"
40-
return msg
26+
return f"{self.http_status}: {self.json_body}"
4127

4228
def __repr__(self):
43-
return f"<CodeBoxError[{self.code}] {self._message}>"
44-
45-
# def construct_error_object(self):
46-
# if (
47-
# self.json_body is None
48-
# or "error" not in self.json_body
49-
# or not isinstance(self.json_body["error"], dict)
50-
# ):
51-
# return None
52-
53-
# return codebox.api_resources.error_object.ErrorObject.construct_from(
54-
# self.json_body["error"]
55-
# ) # TODO: implement
29+
return f"<CodeBoxError {self.http_status}: {self.json_body}>"

codeboxapi/utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ def handle_response(response: requests.Response):
4949
)
5050
if response.status_code != 200:
5151
raise CodeBoxError(
52-
message=f"Error: {response.status_code}",
53-
http_body=response.content,
5452
http_status=response.status_code,
55-
headers=response.headers,
56-
code=response.status_code,
53+
json_body=response.json(),
54+
headers=response.headers.__dict__,
5755
)
5856
return handler(response)
5957

@@ -85,11 +83,9 @@ async def default_handler(r: ClientResponse) -> dict:
8583
)
8684
if response.status != 200:
8785
raise CodeBoxError(
88-
message=f"Error: {response.status}",
89-
http_body=await response.text(),
9086
http_status=response.status,
91-
headers=response.headers,
92-
code=response.status,
87+
json_body=await response.json(),
88+
headers=response.headers.__dict__,
9389
)
9490
return await handler(response)
9591

0 commit comments

Comments
 (0)