diff --git a/elasticsearch/connection/http_requests.py b/elasticsearch/connection/http_requests.py index fc702bb6..372ce375 100644 --- a/elasticsearch/connection/http_requests.py +++ b/elasticsearch/connection/http_requests.py @@ -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) diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py index d03b6620..430154b9 100644 --- a/elasticsearch/connection/http_urllib3.py +++ b/elasticsearch/connection/http_urllib3.py @@ -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) diff --git a/elasticsearch/connection/thrift.py b/elasticsearch/connection/thrift.py index 1fa59449..442606e8 100644 --- a/elasticsearch/connection/thrift.py +++ b/elasticsearch/connection/thrift.py @@ -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: diff --git a/elasticsearch/exceptions.py b/elasticsearch/exceptions.py index 345dd0b7..d62fb5c2 100644 --- a/elasticsearch/exceptions.py +++ b/elasticsearch/exceptions.py @@ -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. """