Commit 391e0f5

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-11-29 16:32:16
chore(networking): enhance `hx_req` to return raw response on failure
1 parent f94f57b
Changed files (1)
src/networking.py
@@ -54,6 +54,7 @@ async def hx_req(
     mobile: bool = False,
     rformat: Literal["json", "text", "content"] = "json",
     last_error: str = "",
+    hx_raw: dict | None = None,
 ) -> dict[str, Any]:
     """Request the given URL with the given method and return the response as a dictionary.
 
@@ -76,13 +77,14 @@ async def hx_req(
         mobile (bool, optional): Whether to use mobile headers.
         rformat (str, optional): The format of the response.
         last_error (str, optional): Last error message.
+        hx_raw (dict, optional): Raw HTTPX response on failure.
 
     Returns:
         dict: {"success": bool, "data": response}
     """
     if retry > max_retry:
         logger.error(f"[{method}] Failed after {retry} retries: {url}")
-        return {"hx_error": last_error}
+        return {"hx_error": last_error, "hx_raw": hx_raw or {}}
     if transport is None:
         transport = AsyncCurlTransport(proxy=proxy, impersonate="safari_ios" if mobile else "chrome", default_headers=True, curl_options={CurlOpt.FRESH_CONNECT: True})
 
@@ -117,13 +119,13 @@ async def hx_req(
     except Exception as e:
         error = f"{type(e).__name__}[{retry + 1}/{max_retry + 1}]: Failed to request {url}, {e}"
         with contextlib.suppress(Exception):
-            error += f"\n{response.json()}"  # type: ignore
+            hx_raw = response.json()  # type: ignore
         if "res" in locals():
             error += f"\n{res}"  # type: ignore
         elif "data" in locals():
             error += f"\n{data}"
         logger.error(error)
-        return await hx_req(url, method, headers=headers, cookies=cookies, params=params, data=data, json_data=json_data, proxy=proxy, follow_redirects=follow_redirects, check_keys=check_keys, check_kv=check_kv, timeout=timeout, retry=retry + 1, max_retry=max_retry, silent=silent, rformat=rformat, last_error=error)  # fmt: off
+        return await hx_req(url, method, headers=headers, cookies=cookies, params=params, data=data, json_data=json_data, proxy=proxy, follow_redirects=follow_redirects, check_keys=check_keys, check_kv=check_kv, timeout=timeout, retry=retry + 1, max_retry=max_retry, silent=silent, rformat=rformat, last_error=error, hx_raw=hx_raw)  # fmt: off
 
 
 async def download_file(