Skip to content

Commit

Permalink
fix: Handle timeout errors separately from "general" ClientError
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Aug 18, 2024
1 parent 7c81663 commit c105bf4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion aspxstats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .exceptions import Error, ClientError, InvalidResponseError, NotFoundError, InvalidParameterError
from .exceptions import Error, ClientError, TimeoutError, InvalidResponseError, NotFoundError, InvalidParameterError
from .types import ResponseValidationMode

"""
Expand All @@ -14,6 +14,7 @@
'ResponseValidationMode',
'Error',
'ClientError',
'TimeoutError',
'InvalidResponseError',
'NotFoundError',
'InvalidParameterError'
Expand Down
5 changes: 4 additions & 1 deletion aspxstats/async_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
from enum import Enum
from typing import Dict, Optional, Union
from urllib.parse import urljoin

import aiohttp as aiohttp

from .client import AspxClient
from .exceptions import ClientError
from .exceptions import ClientError, TimeoutError
from .types import ResponseValidationMode


Expand Down Expand Up @@ -46,5 +47,7 @@ async def get_aspx_data(self, endpoint: str, params: Optional[Dict[str, Optional
return await response.text(errors='replace')
else:
raise ClientError(f'Failed to fetch ASPX data (HTTP/{response.status})')
except asyncio.TimeoutError:
raise TimeoutError('Timed out trying to fetch ASPX data')
except aiohttp.ClientError as e:
raise ClientError(f'Failed to fetch ASPX data: {e}')
4 changes: 3 additions & 1 deletion aspxstats/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import requests as requests

from .exceptions import ClientError, InvalidResponseError, Error
from .exceptions import ClientError, InvalidResponseError, Error, TimeoutError
from .types import LineType, Dataset, ParseTarget, ResponseValidationMode


Expand Down Expand Up @@ -57,6 +57,8 @@ def get_aspx_data(self, endpoint: str, params: Optional[Dict[str, Optional[Union
return response.text
else:
raise ClientError(f'Failed to fetch ASPX data (HTTP/{response.status_code})')
except requests.Timeout:
raise TimeoutError('Timed out trying to fetch ASPX data')
except requests.RequestException as e:
raise ClientError(f'Failed to fetch ASPX data: {e}')

Expand Down
4 changes: 4 additions & 0 deletions aspxstats/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class ClientError(Error):
pass


class TimeoutError(Error):
pass


class InvalidResponseError(Error):
pass

Expand Down

0 comments on commit c105bf4

Please sign in to comment.