Ensure connection error cause is included
This commit is contained in:
1 parent
3a7b468ac8
commit
b05d9bca2d
1 file changed
+15
-10
@@ -736,24 +736,29 @@ class Twitch:
|
|||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def request(
|
async def request(
|
||||||
self, method: str, url: str, **kwargs
|
self, method: str, url: str, *, attempts: int = 5, **kwargs
|
||||||
) -> AsyncIterator[aiohttp.ClientResponse]:
|
) -> AsyncIterator[aiohttp.ClientResponse]:
|
||||||
if self._session is None:
|
if self._session is None:
|
||||||
await self.initialize()
|
await self.initialize()
|
||||||
session = self._session
|
session = self._session
|
||||||
assert session is not None
|
assert session is not None
|
||||||
for attempt in range(5):
|
method = method.upper()
|
||||||
|
cause: Optional[Exception] = None
|
||||||
|
for attempt in range(attempts):
|
||||||
|
logger.debug(f"Request: ({method=}, {url=}, {attempts=}, {kwargs=})")
|
||||||
try:
|
try:
|
||||||
async with session.request(method, url, **kwargs) as response:
|
async with session.request(method, url, **kwargs) as response:
|
||||||
|
logger.debug(f"Response: {response.status}: {response}")
|
||||||
yield response
|
yield response
|
||||||
break
|
return
|
||||||
except (aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError):
|
except aiohttp.ClientConnectionError as exc:
|
||||||
await asyncio.sleep(0.1 * attempt)
|
cause = exc
|
||||||
else:
|
if attempt < attempts - 1:
|
||||||
raise RequestException(
|
await asyncio.sleep(0.1 * attempt)
|
||||||
"Ran out of attempts while handling a request: "
|
raise RequestException(
|
||||||
f"(method={method.upper()}, {url=}, {kwargs=})"
|
"Ran out of attempts while handling a request: "
|
||||||
)
|
f"({method=}, {url=}, {attempts=}, {kwargs=})"
|
||||||
|
) from cause
|
||||||
|
|
||||||
async def gql_request(self, op: GQLOperation) -> JsonType:
|
async def gql_request(self, op: GQLOperation) -> JsonType:
|
||||||
headers = {
|
headers = {
|
||||||
|
|||||||
Reference in new issue
Block a user