Introducing a ConnectionTimeout as a subclass of ConnectionError

This is to enable #128 to work
This commit is contained in:
Honza Král
2014-10-03 18:49:07 +02:00
parent f401d100a8
commit ecfeb93a6f
4 changed files with 20 additions and 3 deletions
+5 -2
View File
@@ -6,7 +6,7 @@ except ImportError:
REQUESTS_AVAILABLE = False
from .base import Connection
from ..exceptions import ConnectionError, ImproperlyConfigured
from ..exceptions import ConnectionError, ImproperlyConfigured, ConnectionTimeout
from ..compat import urlencode
class RequestsHttpConnection(Connection):
@@ -44,7 +44,10 @@ class RequestsHttpConnection(Connection):
response = self.session.request(method, url, data=body, timeout=timeout or self.timeout)
duration = time.time() - start
raw_data = response.text
except (requests.ConnectionError, requests.Timeout) as e:
except requests.Timeout as e:
self.log_request_fail(method, url, body, time.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except requests.ConnectionError as e:
self.log_request_fail(method, url, body, time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
+4
View File
@@ -1,5 +1,6 @@
import time
import urllib3
from urllib3.exceptions import ReadTimeoutError
from .base import Connection
from ..exceptions import ConnectionError
@@ -49,6 +50,9 @@ class Urllib3HttpConnection(Connection):
response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw)
duration = time.time() - start
raw_data = response.data.decode('utf-8')
except ReadTimeoutError as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise ConnectionError('TIMEOUT', str(e), e)
except Exception as e:
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
+4 -1
View File
@@ -14,7 +14,7 @@ try:
except ImportError:
THRIFT_AVAILABLE = False
from ..exceptions import ConnectionError, ImproperlyConfigured
from ..exceptions import ConnectionError, ImproperlyConfigured, ConnectionTimeout
from .pooling import PoolingConnection
logger = logging.getLogger('elasticsearch')
@@ -66,6 +66,9 @@ class ThriftConnection(PoolingConnection):
tclient = self._get_connection()
response = tclient.execute(request)
duration = time.time() - start
except SocketTimeout as e:
self.log_request_fail(method, url, body, time.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except (TException, SocketTimeout) as e:
self.log_request_fail(method, url, body, time.time() - start, exception=e)
if tclient:
+7
View File
@@ -61,6 +61,13 @@ class ConnectionError(TransportError):
self.error, self.info.__class__.__name__, self.info)
class ConnectionTimeout(ConnectionError):
""" A network timeout. """
def __str__(self):
return 'ConnectionTimeout caused by - %s(%s)' % (
self.info.__class__.__name__, self.info)
class NotFoundError(TransportError):
""" Exception representing a 404 status code. """