Skip to content

Commit

Permalink
Improved API Error reporting & bumped to 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagobasulto committed Apr 26, 2023
1 parent 3b6f191 commit 309170e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
32 changes: 22 additions & 10 deletions ipython_gpt/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ class APIClientException(Exception):


class APIResponseException(APIClientException):
def __init__(self, method, path, headers, query_params, body):
def __init__(self, method, path, response_status, response_body):
self.method = method
self.path = path
self.headers = headers
self.query_params = query_params
self.body = body
self.response_status = response_status
self.response_body = response_body

def __str__(self):
return f"Failed API Request '{self.method} {self.path}'"
error_message = (
(self.response_body or {})
.get("error", {})
.get("message", "No response from API.")
)
return f"Failed API Request '{self.method} {self.path}'. Got {self.response_status}: {error_message}"


class UnauthorizedAPIException(APIResponseException):
pass


class OpenAIClient:
Expand Down Expand Up @@ -54,12 +62,16 @@ def request(self, method, path, headers=None, query_params=None, json_body=None)
try:
connection.request(method, path, body, headers)
resp = connection.getresponse()
resp_body = resp.read()

# TODO: this might raise an exception for an invalid body
content = json.loads(resp_body.decode("utf-8"))
if 200 <= resp.status < 300:
resp_body = resp.read()
if resp_body and len(resp_body) > 0:
return json.loads(resp_body.decode("utf-8"))
else:
raise APIResponseException(method, path, headers, query_params, body)
return content
if resp.status == 401:
raise UnauthorizedAPIException(method, path, resp.status, content)

# Catch all exception for any other not known status
raise APIResponseException(method, path, resp.status, content)
finally:
connection.close()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ipython-gpt"
version = "0.0.5"
version = "0.0.6"
description = "A Jupyter/IPython extension to use ChatGPT"
authors = ["Santiago Basulto <santiago.basulto@gmail.com>"]
readme = "README.md"
Expand Down
4 changes: 3 additions & 1 deletion tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def test_api_client_auth():

with patch.object(HTTPSConnection, "request") as mocked_request:
mock = Mock(status=200)
mock.read.return_value = ""
mock.read.return_value = '{"choices": {"message": "Valid response"}}'.encode(
"utf-8"
)
with patch.object(HTTPSConnection, "getresponse", return_value=mock):
client = OpenAIClient("VERY SECRET KEY")
client.request("POST", "/chat/completions", json_body=json_body)
Expand Down

0 comments on commit 309170e

Please sign in to comment.