Skip to content

Commit

Permalink
Improved debug by logging 'failed after X seconds'.
Browse files Browse the repository at this point in the history
  • Loading branch information
sander1988 committed May 24, 2024
1 parent ab352bd commit c127c6e
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions pyIndego/indego_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging
import json
import time
from socket import error as SocketError
from typing import Any, Optional, Callable, Awaitable

Expand Down Expand Up @@ -494,6 +495,7 @@ async def _request( # noqa: C901
headers = self._default_headers.copy()
headers["Authorization"] = "Bearer %s" % self._token

request_start_time = None
try:
log_headers = headers.copy()
if 'Authorization' in log_headers:
Expand All @@ -506,6 +508,7 @@ async def _request( # noqa: C901
json.dumps(data) if data is not None else '',
)

request_start_time = time.time()
async with self._session.request(
method=method.value,
url=url,
Expand Down Expand Up @@ -536,7 +539,13 @@ async def _request( # noqa: C901
response.raise_for_status()

except (asyncio.TimeoutError, ServerTimeoutError, HTTPGatewayTimeout) as exc:
_LOGGER.info("%s %s request timed out (mower offline?): %s. Retrying...", method.value, path, exc)
_LOGGER.info(
"%s %s request timed out after %i seconds (mower offline?): %s. Retrying...",
method.value,
path,
time.time() - request_start_time,
str(exc)
)
return await self._request(
method=method,
path=path,
Expand All @@ -546,13 +555,25 @@ async def _request( # noqa: C901
)

except ClientOSError as exc:
_LOGGER.debug("%s %s request timed out: %s", method.value, path, exc)
_LOGGER.debug(
"%s %s request timed out after %i seconds: %s",
method.value,
path,
time.time() - request_start_time,
str(exc)
)
return None

except (TooManyRedirects, ClientResponseError, SocketError) as exc:
if self._raise_request_exceptions:
raise
_LOGGER.error("%s %s failed: %s", method.value, path, exc)
_LOGGER.error(
"%s %s failed after %i seconds: %s",
method.value,
path,
time.time() - request_start_time,
str(exc)
)
return None

except asyncio.CancelledError:
Expand All @@ -562,7 +583,12 @@ async def _request( # noqa: C901
except Exception as exc:
if self._raise_request_exceptions:
raise
_LOGGER.error("Request %s %s gave a unhandled error: %s", method.value, path, exc)
_LOGGER.error(
"Request %s %s gave a unhandled error: %s",
method.value,
path,
str(exc)
)
return None

async def get(self, path: str, timeout: int = 30):
Expand Down

0 comments on commit c127c6e

Please sign in to comment.