From 8ce3370080ba265f62c4010f31a2b2ae51706a7d Mon Sep 17 00:00:00 2001 From: Randall Leeds Date: Tue, 10 Feb 2015 17:04:48 -0800 Subject: [PATCH] Ignore 404 on fetch to avoid a log message Otherwise, elasticsearch will emit a log message even though we ignore the exception. --- annotator/elasticsearch.py | 13 ++++++------- tests/test_elasticsearch.py | 4 +--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/annotator/elasticsearch.py b/annotator/elasticsearch.py index f773d06..3d4c532 100644 --- a/annotator/elasticsearch.py +++ b/annotator/elasticsearch.py @@ -131,13 +131,12 @@ def drop_all(cls): # already define that method name. @classmethod def fetch(cls, id): - try: - doc = cls.es.conn.get(index=cls.es.index, - doc_type=cls.__type__, - id=id) - except elasticsearch.exceptions.NotFoundError: - return None - return cls(doc['_source'], id=id) + doc = cls.es.conn.get(index=cls.es.index, + doc_type=cls.__type__, + ignore=404, + id=id) + if doc.get('found', True): + return cls(doc['_source'], id=id) @classmethod def _build_query(cls, query=None, offset=None, limit=None, sort=None, order=None): diff --git a/tests/test_elasticsearch.py b/tests/test_elasticsearch.py index dc839f5..f6cc3b6 100644 --- a/tests/test_elasticsearch.py +++ b/tests/test_elasticsearch.py @@ -57,9 +57,7 @@ def test_fetch(self, es_mock): @patch('annotator.elasticsearch.elasticsearch.Elasticsearch') def test_fetch_not_found(self, es_mock): conn = es_mock.return_value - def raise_exc(*args, **kwargs): - raise elasticsearch.exceptions.NotFoundError('foo') - conn.get.side_effect = raise_exc + conn.get.return_value = {'found': False} o = self.Model.fetch(123) assert_equal(o, None)