diff --git a/elasticsearch/exceptions.py b/elasticsearch/exceptions.py index be45fb10..8db34719 100644 --- a/elasticsearch/exceptions.py +++ b/elasticsearch/exceptions.py @@ -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, } diff --git a/test_elasticsearch/test_connection.py b/test_elasticsearch/test_connection.py index 7c55f07c..e63bd4fb 100644 --- a/test_elasticsearch/test_connection.py +++ b/test_elasticsearch/test_connection.py @@ -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('', 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): diff --git a/test_elasticsearch/test_server/test_common.py b/test_elasticsearch/test_server/test_common.py index 3daebb1d..a9c89899 100644 --- a/test_elasticsearch/test_server/test_common.py +++ b/test_elasticsearch/test_server/test_common.py @@ -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():