|
21 | 21 |
|
22 | 22 | import requests |
23 | 23 |
|
24 | | -__all__ = ["LpdbDataType", "LpdbError", "LpdbWarning", "LpdbSession"] |
| 24 | +__all__ = [ |
| 25 | + "LpdbDataType", |
| 26 | + "LpdbError", |
| 27 | + "LpdbInvalidKeyError", |
| 28 | + "LpdbInvalidRequestError", |
| 29 | + "LpdbRateLimitError", |
| 30 | + "LpdbWarning", |
| 31 | + "LpdbSession", |
| 32 | +] |
25 | 33 |
|
26 | 34 | _PACKAGE_NAME: Final[str] = "lpdb_python" |
27 | 35 |
|
@@ -82,6 +90,22 @@ class LpdbError(IOError): |
82 | 90 | pass |
83 | 91 |
|
84 | 92 |
|
| 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 | + |
85 | 109 | class LpdbRateLimitError(LpdbError): |
86 | 110 | """ |
87 | 111 | Raised when the LPDB request failed due to a rate limit. |
@@ -301,19 +325,19 @@ def _parse_results( |
301 | 325 | lpdb_warnings = response.get("warning") |
302 | 326 | lpdb_errors = response.get("error") |
303 | 327 |
|
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 | + ) |
310 | 338 | raise LpdbRateLimitError( |
311 | 339 | wiki=rate_limit.group("wiki"), table=rate_limit.group("table") |
312 | 340 | ) |
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}") |
317 | 341 | if lpdb_warnings and len(lpdb_warnings) != 0: |
318 | 342 | for lpdb_warning in lpdb_warnings: |
319 | 343 | warnings.warn(lpdb_warning, LpdbWarning) |
|
0 commit comments