Skip to content

Commit

Permalink
Del RecoverableServerError, del retry middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-stakewise committed Sep 11, 2023
1 parent 67203b5 commit c1a3a5a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
13 changes: 9 additions & 4 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from web3.types import Timestamp

from sw_utils.common import urljoin
from sw_utils.decorators import retry_aiohttp_errors
from sw_utils.exceptions import AiohttpRecoveredErrors
from sw_utils.decorators import can_be_retried_aiohttp_error, retry_aiohttp_errors
from sw_utils.typings import ChainHead, ConsensusFork

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -91,7 +90,10 @@ async def submit_voluntary_exit(
response.raise_for_status()
return

except AiohttpRecoveredErrors as error:
except Exception as error:
if not can_be_retried_aiohttp_error(error):
raise error

if i == len(self.base_urls) - 1:
raise error
logger.error('%s: %s', url, repr(error))
Expand Down Expand Up @@ -152,7 +154,10 @@ async def _async_make_get_request_inner(self, endpoint_uri: str) -> dict[str, An
uri = URI(urljoin(url, endpoint_uri))
return await async_json_make_get_request(uri, timeout=self.timeout)

except AiohttpRecoveredErrors as error:
except Exception as error:
if not can_be_retried_aiohttp_error(error):
raise error

if i == len(self.base_urls) - 1:
raise error
logger.error('%s: %s', url, repr(error))
Expand Down
28 changes: 1 addition & 27 deletions sw_utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,12 @@
logger = logging.getLogger(__name__)


class RecoverableServerError(Exception):
"""
Wrapper around ClientResponseError for HTTP 500 errors.
Only for internal use inside sw-utils library.
Do not raise `RecoverableServerError` in application code.
"""

def __init__(self, origin: requests.HTTPError | aiohttp.ClientResponseError):
self.origin = origin
if isinstance(origin, requests.HTTPError):
self.status_code = origin.response.status_code
self.uri = origin.response.url
elif isinstance(origin, aiohttp.ClientResponseError):
self.status_code = origin.status
self.uri = origin.request_info

super().__init__()

def __str__(self):
return (
f'RecoverableServerError (status_code: {self.status_code}, '
f'uri: {self.uri}): {self.origin}'
)


# DEPRECATED
AiohttpRecoveredErrors = (
aiohttp.ClientConnectionError,
RecoverableServerError,
asyncio.TimeoutError,
)
RequestsRecoveredErrors = (
requests.ConnectionError,
requests.Timeout,
RecoverableServerError,
)
12 changes: 8 additions & 4 deletions sw_utils/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from web3.middleware import async_geth_poa_middleware
from web3.net import AsyncNet
from web3.providers.async_rpc import AsyncHTTPProvider
from web3.types import RPCEndpoint, RPCResponse, Wei
from web3.types import AsyncMiddleware, RPCEndpoint, RPCResponse, Wei

from sw_utils.decorators import retry_aiohttp_errors
from sw_utils.exceptions import AiohttpRecoveredErrors
from sw_utils.decorators import can_be_retried_aiohttp_error, retry_aiohttp_errors

logger = logging.getLogger(__name__)

Expand All @@ -34,6 +33,7 @@ class ExtendedAsyncHTTPProvider(AsyncHTTPProvider):

_providers: list[AsyncHTTPProvider] = []
_locker_provider: AsyncHTTPProvider | None = None
_middlewares: tuple[AsyncMiddleware, ...] = ()

def __init__(
self,
Expand Down Expand Up @@ -83,9 +83,13 @@ async def make_request_inner(self, method: RPCEndpoint, params: Any) -> RPCRespo
try:
response = await provider.make_request(method, params)
return response
except AiohttpRecoveredErrors as error:
except Exception as error:
if not can_be_retried_aiohttp_error(error):
raise error

if i == len(self._providers) - 1:
raise error

logger.error('%s: %s', provider.endpoint_uri, repr(error))

return {}
Expand Down

0 comments on commit c1a3a5a

Please sign in to comment.