From c1a3a5ac9dee559dab86a50ff70d050fe533f075 Mon Sep 17 00:00:00 2001 From: Evgeny Gusarov Date: Wed, 23 Aug 2023 23:03:31 +0300 Subject: [PATCH] Del RecoverableServerError, del retry middleware --- sw_utils/consensus.py | 13 +++++++++---- sw_utils/exceptions.py | 28 +--------------------------- sw_utils/execution.py | 12 ++++++++---- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/sw_utils/consensus.py b/sw_utils/consensus.py index 8577d25..370cc23 100644 --- a/sw_utils/consensus.py +++ b/sw_utils/consensus.py @@ -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__) @@ -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)) @@ -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)) diff --git a/sw_utils/exceptions.py b/sw_utils/exceptions.py index 5900683..72f8695 100644 --- a/sw_utils/exceptions.py +++ b/sw_utils/exceptions.py @@ -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, ) diff --git a/sw_utils/execution.py b/sw_utils/execution.py index 01d4b32..4143cfa 100644 --- a/sw_utils/execution.py +++ b/sw_utils/execution.py @@ -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__) @@ -34,6 +33,7 @@ class ExtendedAsyncHTTPProvider(AsyncHTTPProvider): _providers: list[AsyncHTTPProvider] = [] _locker_provider: AsyncHTTPProvider | None = None + _middlewares: tuple[AsyncMiddleware, ...] = () def __init__( self, @@ -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 {}