Skip to content

Commit ef87b80

Browse files
committed
clean up non-200 http code handling
1 parent ddd299d commit ef87b80

2 files changed

Lines changed: 11 additions & 15 deletions

File tree

src/lpdb/async_session/async_session.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,9 @@ async def get_wikis() -> set[str]:
6060
async def __handle_response(
6161
response: aiohttp.ClientResponse,
6262
) -> list[dict[str, Any]]:
63-
try:
64-
return AbstractLpdbSession._parse_results(await response.json())
65-
except Exception as e:
66-
if isinstance(e, LpdbError):
67-
raise e
68-
status = HTTPStatus(response.status)
69-
raise LpdbError(f"HTTP {status}: {status.name}") from e
63+
return AbstractLpdbSession._parse_results(
64+
response.status, await response.json()
65+
)
7066

7167
@override
7268
async def make_request(

src/lpdb/session.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ def _parse_params(
277277
return parameters
278278

279279
@staticmethod
280-
def _parse_results(response: LpdbResponse) -> list[dict[str, Any]]:
280+
def _parse_results(
281+
status_code: int, response: LpdbResponse
282+
) -> list[dict[str, Any]]:
281283
result = response["result"]
282284
lpdb_warnings = response.get("warning")
283285
lpdb_errors = response.get("error")
@@ -292,6 +294,9 @@ def _parse_results(response: LpdbResponse) -> list[dict[str, Any]]:
292294
wiki=rate_limit.group("wiki"), table=rate_limit.group("table")
293295
)
294296
raise LpdbError(re.sub(r"^Error: ?", "", lpdb_errors[0]))
297+
elif status_code != HTTPStatus.OK:
298+
status = HTTPStatus(status_code)
299+
raise LpdbError(f"HTTP {status_code}: {status.name}")
295300
if lpdb_warnings and len(lpdb_warnings) != 0:
296301
for lpdb_warning in lpdb_warnings:
297302
warnings.warn(lpdb_warning, LpdbWarning)
@@ -315,13 +320,8 @@ def get_wikis() -> set[str]:
315320

316321
@staticmethod
317322
def __handle_response(response: requests.Response) -> list[dict[str, Any]]:
318-
try:
319-
return AbstractLpdbSession._parse_results(response.json())
320-
except Exception as e:
321-
if isinstance(e, LpdbError):
322-
raise e
323-
status = HTTPStatus(response.status_code)
324-
raise LpdbError(f"HTTP {status}: {status.name}") from e
323+
status = HTTPStatus(response.status_code)
324+
return AbstractLpdbSession._parse_results(status, response.json())
325325

326326
@override
327327
def make_request(

0 commit comments

Comments
 (0)