diff --git a/oedipus/__init__.py b/oedipus/__init__.py index 2a8e8ca..02b0dd7 100644 --- a/oedipus/__init__.py +++ b/oedipus/__init__.py @@ -402,6 +402,7 @@ def _filter_value_to_int(self, name, value): def _sanitize_query(query): """Strip control characters that cause problems.""" query = re.sub(r'(?<=\S)\-', '\-', query) + query = query.replace('/', '\\/') return query.replace('^', '').replace('$', '') def _sphinx(self): @@ -543,8 +544,9 @@ def _raw(self): if not results: raise SearchError('Sphinx returned no results.') if results[0]['status'] == sphinxapi.SEARCHD_ERROR: - raise SearchError('Sphinx had an error while performing a ' - 'query.') + log.error('Sphinx errored while performing a query: %r', + results[0]['error']) + return {'matches': []} # We do only one query at a time; return the first one: return self._results_cache[0] diff --git a/oedipus/tests/test_misc.py b/oedipus/tests/test_misc.py index 05cbe86..bd69f65 100644 --- a/oedipus/tests/test_misc.py +++ b/oedipus/tests/test_misc.py @@ -42,6 +42,18 @@ def test_connection_failure(sphinx_client): assert_raises(SearchError, S(Biscuit)._raw) +@fudge.patch('sphinxapi.SphinxClient') +def test_query_error(sphinx_client): + """[] should get returned when the query has an error.""" + (sphinx_client.expects_call().returns_fake() + .is_a_stub() + .expects('RunQueries').returns([{ + 'status': 1, + 'warning': '', + 'error': 'index questions: syntax error'}])) + eq_(S(Biscuit)._raw(), {'matches': []}) + + @fudge.patch('sphinxapi.SphinxClient') @fudge.patch('oedipus.settings') def test_sphinx_max_results_clips(sphinx_client, settings): @@ -57,3 +69,9 @@ def test_sphinx_max_results_clips(sphinx_client, settings): s = S(Biscuit)[0:] # Do this to trigger the results. s.count() + + +def test_sanitize_query(): + """Tests _sanitize_query.""" + sq = S._sanitize_query + eq_(sq('google.com/iq'), 'google.com\\/iq')