Skip to content

Commit b325eb8

Browse files
Merge pull request #2 from Infuzu/dev
feat: enhance error handling and type annotations
2 parents 31d17a7 + 4688306 commit b325eb8

4 files changed

Lines changed: 55 additions & 9 deletions

File tree

main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from src.infuzu import create_chat_completion, ChatCompletionsHandlerRequestMessage
1+
from src.infuzu import (create_chat_completion, ChatCompletionsHandlerRequestMessage)
22
from dotenv import load_dotenv
33

44

55
load_dotenv()
66

77

8-
messages = [
8+
messages: list[ChatCompletionsHandlerRequestMessage] = [
99
ChatCompletionsHandlerRequestMessage(role="system", content="You are a helpful assistant."),
1010
ChatCompletionsHandlerRequestMessage(role="user", content="What is the capital of France?"),
1111
]
1212

1313

1414
try:
15-
response = create_chat_completion(messages=messages)
15+
response: dict[str, any] = create_chat_completion(messages=messages)
1616
print(response)
1717
except Exception as e:
1818
print(f"Error: {e}")

src/infuzu/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
from .api_client import create_chat_completion, ChatCompletionsHandlerRequestMessage, ChatCompletionsRequestContentPart
1+
from .api_client import (
2+
create_chat_completion, ChatCompletionsHandlerRequestMessage, ChatCompletionsRequestContentPart
3+
)
4+
from .errors import (InfuzuAPIError, APIWarning, APIError)
25

36
__all__: list[str] = [
47
"create_chat_completion",
58
"ChatCompletionsRequestContentPart",
6-
"ChatCompletionsHandlerRequestMessage"
9+
"ChatCompletionsHandlerRequestMessage",
10+
11+
"InfuzuAPIError",
12+
"APIWarning",
13+
"APIError",
714
]

src/infuzu/api_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,5 @@ def create_chat_completion(
122122

123123
response.raise_for_status()
124124
return response.json()
125-
126125
except httpx.HTTPStatusError as e:
127126
raise InfuzuAPIError(e)

src/infuzu/errors.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
1+
import logging
2+
from json import JSONDecodeError
3+
from typing import Optional
14
import httpx
5+
from pydantic import BaseModel
26

37

4-
class InfuzuAPIError(Exception):
5-
def __init__(self, base_error: httpx.HTTPError) -> None:
6-
super().__init__(f"HTTP Error: {str(base_error)}")
8+
logger: logging.Logger = logging.getLogger(__name__)
9+
10+
11+
class APIError(BaseModel):
12+
code: Optional[str] = None
13+
message: Optional[str] = None
14+
15+
class Config:
16+
extra: str = "allow"
17+
18+
19+
class APIWarning(BaseModel):
20+
code: Optional[str] = None
21+
message: Optional[str] = None
22+
23+
class Config:
24+
extra: str = "allow"
25+
26+
27+
class InfuzuAPIError(httpx.HTTPStatusError):
28+
def __init__(self, base_error: httpx.HTTPStatusError) -> None:
29+
super().__init__(
30+
f"Infuzu API Error: {base_error}", request=base_error.request, response=base_error.response
31+
)
32+
33+
try:
34+
self.response_json: dict[str, any] = self.response.json()
35+
except JSONDecodeError as e:
36+
logger.warning(f"Failed to decode JSON response: {e}")
37+
self.response_json: dict[str, any] = {}
38+
39+
self.results: dict[str, any] = self.response_json.get("results", {})
40+
41+
self.errors: list[APIError] = [APIError(**error) for error in self.response_json.get("errors", [])]
42+
43+
self.warnings: list[APIWarning] = [APIWarning(**warning) for warning in self.response_json.get("warnings", [])]
44+
45+
def __str__(self) -> str:
46+
return f"{super().__str__()} | Errors: {self.errors} | Warnings: {self.warnings} | Results: {self.results}"

0 commit comments

Comments
 (0)