Added more detailed error handling
namely being able to detect conflicts and bad requests
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
__all__ = ['ImproperlyConfigured', 'ElasticsearchException', 'SerializationError', 'TransportError', 'NotFoundError']
|
||||
__all__ = [
|
||||
'ImproperlyConfigured', 'ElasticsearchException', 'SerializationError',
|
||||
'TransportError', 'NotFoundError', 'ConflictError', 'RequestError'
|
||||
]
|
||||
|
||||
class ImproperlyConfigured(Exception):
|
||||
pass
|
||||
@@ -43,7 +46,16 @@ class NotFoundError(TransportError):
|
||||
pass
|
||||
|
||||
|
||||
class ConflictError(TransportError):
|
||||
pass
|
||||
|
||||
|
||||
class RequestError(TransportError):
|
||||
pass
|
||||
|
||||
# more generic mappings from status_code to python exceptions
|
||||
HTTP_EXCEPTIONS = {
|
||||
400: RequestError,
|
||||
404: NotFoundError,
|
||||
409: ConflictError,
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import re
|
||||
from mock import Mock, patch
|
||||
import urllib3
|
||||
|
||||
from elasticsearch.exceptions import TransportError
|
||||
from elasticsearch.exceptions import TransportError, ConflictError, RequestError, NotFoundError
|
||||
from elasticsearch.connection import RequestsHttpConnection, \
|
||||
Urllib3HttpConnection, THRIFT_AVAILABLE, ThriftConnection
|
||||
|
||||
@@ -96,6 +96,18 @@ class TestRequestsConnection(TestCase):
|
||||
con = self._get_mock_connection({"host": "elasticsearch.com", "port": 443})
|
||||
self.assertEquals('<RequestsHttpConnection: http://elasticsearch.com:443>', repr(con))
|
||||
|
||||
def test_conflict_error_is_returned_on_409(self):
|
||||
con = self._get_mock_connection(status_code=409)
|
||||
self.assertRaises(ConflictError, con.perform_request, 'GET', '/', {}, '')
|
||||
|
||||
def test_not_found_error_is_returned_on_404(self):
|
||||
con = self._get_mock_connection(status_code=404)
|
||||
self.assertRaises(NotFoundError, con.perform_request, 'GET', '/', {}, '')
|
||||
|
||||
def test_request_error_is_returned_on_400(self):
|
||||
con = self._get_mock_connection(status_code=400)
|
||||
self.assertRaises(RequestError, con.perform_request, 'GET', '/', {}, '')
|
||||
|
||||
@patch('elasticsearch.connection.base.tracer')
|
||||
@patch('elasticsearch.connection.base.logger')
|
||||
def test_failed_request_logs_and_traces(self, logger, tracer):
|
||||
|
||||
@@ -7,6 +7,8 @@ from os import walk, environ
|
||||
from os.path import exists, join, dirname, pardir
|
||||
import yaml
|
||||
|
||||
from elasticsearch import TransportError
|
||||
|
||||
from ..test_cases import SkipTest
|
||||
from . import ElasticTestCase
|
||||
|
||||
@@ -14,11 +16,16 @@ from . import ElasticTestCase
|
||||
# those in the tests accordingly
|
||||
PARAMS_RENAMES = {
|
||||
'type': 'doc_type',
|
||||
'from': 'offset',
|
||||
'from': 'from_',
|
||||
}
|
||||
|
||||
ES_VERSION = None
|
||||
|
||||
# mapping from catch values to http status codes
|
||||
CATCH_CODES = {
|
||||
'missing': 404,
|
||||
'conflict': 409,
|
||||
}
|
||||
|
||||
class InvalidActionType(Exception):
|
||||
pass
|
||||
@@ -105,10 +112,10 @@ class YamlTestCase(ElasticTestCase):
|
||||
|
||||
try:
|
||||
self.last_response = api(**args)
|
||||
except:
|
||||
except Exception as e:
|
||||
if not catch:
|
||||
raise
|
||||
self.run_catch(catch)
|
||||
self.run_catch(catch, e)
|
||||
else:
|
||||
if catch:
|
||||
raise AssertionError('Failed to catch %r in %r.' % (catch, self.last_response))
|
||||
@@ -122,8 +129,10 @@ class YamlTestCase(ElasticTestCase):
|
||||
raise SkipTest(reason)
|
||||
|
||||
|
||||
def run_catch(self, catch):
|
||||
pass
|
||||
def run_catch(self, catch, exception):
|
||||
self.assertIsInstance(exception, TransportError)
|
||||
if catch in CATCH_CODES:
|
||||
self.assertEquals(CATCH_CODES[catch], exception.status_code)
|
||||
|
||||
def run_gt(self, action):
|
||||
for key, value in action.items():
|
||||
|
||||
Reference in New Issue
Block a user