Skip to content

Commit 4f5ea3c

Browse files
committed
better error reporting
1 parent 3e5ea9a commit 4f5ea3c

2 files changed

Lines changed: 46 additions & 12 deletions

File tree

src/lpdb_python/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
Transfer,
2525
TeamTemplate,
2626
)
27-
from .session import LpdbError, LpdbWarning, LpdbSession
27+
from .session import (
28+
LpdbError,
29+
LpdbInvalidKeyError,
30+
LpdbInvalidRequestError,
31+
LpdbRateLimitError,
32+
LpdbWarning,
33+
LpdbSession,
34+
)
2835

2936
__all__ = [
3037
"OpponentType",
@@ -33,6 +40,9 @@
3340
"Datapoint",
3441
"ExternalMediaLink",
3542
"LpdbError",
43+
"LpdbInvalidKeyError",
44+
"LpdbInvalidRequestError",
45+
"LpdbRateLimitError",
3646
"LpdbWarning",
3747
"LpdbSession",
3848
"Match",

src/lpdb_python/session.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@
2121

2222
import requests
2323

24-
__all__ = ["LpdbDataType", "LpdbError", "LpdbWarning", "LpdbSession"]
24+
__all__ = [
25+
"LpdbDataType",
26+
"LpdbError",
27+
"LpdbInvalidKeyError",
28+
"LpdbInvalidRequestError",
29+
"LpdbRateLimitError",
30+
"LpdbWarning",
31+
"LpdbSession",
32+
]
2533

2634
_PACKAGE_NAME: Final[str] = "lpdb_python"
2735

@@ -82,6 +90,22 @@ class LpdbError(IOError):
8290
pass
8391

8492

93+
class LpdbInvalidKeyError(LpdbError):
94+
"""
95+
Raised when the LPDB request failed due to an invalid key.
96+
"""
97+
98+
pass
99+
100+
101+
class LpdbInvalidRequestError(LpdbError):
102+
"""
103+
Raised when the LPDB request failed due to an invalid request.
104+
"""
105+
106+
pass
107+
108+
85109
class LpdbRateLimitError(LpdbError):
86110
"""
87111
Raised when the LPDB request failed due to a rate limit.
@@ -301,19 +325,19 @@ def _parse_results(
301325
lpdb_warnings = response.get("warning")
302326
lpdb_errors = response.get("error")
303327

304-
if lpdb_errors and len(lpdb_errors) != 0:
305-
rate_limit = re.match(
306-
r"API key \"[0-9A-Za-z]+\" limits for wiki \"(?P<wiki>[a-z]+)\" and table \"(?P<table>[a-z]+)\" exceeded\.",
307-
lpdb_errors[0],
308-
)
309-
if rate_limit:
328+
match status_code:
329+
case HTTPStatus.FORBIDDEN:
330+
raise LpdbInvalidKeyError(lpdb_errors[0])
331+
case HTTPStatus.NOT_FOUND:
332+
raise LpdbInvalidRequestError(lpdb_errors[0])
333+
case HTTPStatus.TOO_MANY_REQUESTS:
334+
rate_limit = re.match(
335+
r"API key \"[0-9A-Za-z]+\" limits for wiki \"(?P<wiki>[a-z]+)\" and table \"(?P<table>[a-z]+)\" exceeded\.",
336+
lpdb_errors[0],
337+
)
310338
raise LpdbRateLimitError(
311339
wiki=rate_limit.group("wiki"), table=rate_limit.group("table")
312340
)
313-
raise LpdbError(re.sub(r"^Error: ?", "", lpdb_errors[0]))
314-
elif status_code != HTTPStatus.OK:
315-
status = HTTPStatus(status_code)
316-
raise LpdbError(f"HTTP {status_code}: {status.name}")
317341
if lpdb_warnings and len(lpdb_warnings) != 0:
318342
for lpdb_warning in lpdb_warnings:
319343
warnings.warn(lpdb_warning, LpdbWarning)

0 commit comments

Comments
 (0)