Skip to content

Commit 6c3bd1b

Browse files
committed
feat: include http error code in HytaleAPIError
1 parent 551426c commit 6c3bd1b

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with the exception that all versions are currently `0.x.x` and may include break
2323

2424
- Use response headers to determine whether to parse HTTP responses into JSON
2525
- get() can also return `list` type as JSON can also be a list
26+
- Include response status code in HytaleAPIError
2627

2728
### Fixed
2829

src/hytale/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class HytaleAPIError(Exception):
22
"""Base exception for Hytale API errors."""
33

4+
def __init__(self, message: str, http_code: int | None):
5+
super().__init__(message)
6+
self.http_code = http_code
7+
48

59
class BlockedError(HytaleAPIError):
610
"""Exception for when access is blocked by Cloudflare."""

src/hytale/http.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
_session.headers.update(
1515
{
1616
"User-Agent": DEFAULT_USER_AGENT,
17-
"Accept": "application/json",
17+
"Accept": "application/json", # expect JSON but can handle other responses
1818
}
1919
)
2020

@@ -48,14 +48,12 @@ def get(
4848
timeout=3,
4949
)
5050
except requests.RequestException as exc:
51-
raise HytaleAPIError(str(exc)) from exc
51+
raise HytaleAPIError(str(exc), None) from exc
5252

5353
if not response.ok:
5454
if "Attention Required! | Cloudflare" in response.text:
55-
raise BlockedError("This IP is blocked")
56-
raise HytaleAPIError(
57-
f"Request failed [{response.status_code}]: {response.text}"
58-
)
55+
raise BlockedError("This IP is blocked", response.status_code)
56+
raise HytaleAPIError(f"Request failed: {response.text}", response.status_code)
5957

6058
content_type = response.headers.get("Content-Type", "")
6159

0 commit comments

Comments
 (0)