This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 0640949accc798d76e5f71d3cffb44e7a914e498
Author: Dave Brondsema <[email protected]>
AuthorDate: Tue May 26 15:12:53 2026 -0400

    [#8607] solr: syntax problems deserve an escaped retry; don't expose 
underlying error messages
---
 Allura/allura/lib/search.py           | 26 ++++++++++++++++++--------
 Allura/allura/tests/unit/test_solr.py | 15 +++++++++++++++
 2 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/Allura/allura/lib/search.py b/Allura/allura/lib/search.py
index a2371ad7b..4e832261b 100644
--- a/Allura/allura/lib/search.py
+++ b/Allura/allura/lib/search.py
@@ -173,20 +173,30 @@ def strip_local_params(q):
     return q
 
 
-def search(q, short_timeout=False, ignore_errors=True, **kw):
+def search(q, short_timeout=False, ignore_errors=True, search_fn=None, **kw):
     q = inject_user(q)
     q = strip_local_params(q)
-    try:
+    if not search_fn:
         if short_timeout:
-            return g.solr_short_timeout.search(q, **kw)
+            search_fn = g.solr_short_timeout.search
         else:
-            return g.solr.search(q, **kw)
-    except (SolrError, OSError) as e:
+            search_fn = g.solr.search
+    try:
+        # try once with opportunity to retry
+        try:
+            return search_fn(q, **kw)
+        except SolrError:
+            escaped_q = escape_solr_arg(q)
+            if q != escaped_q:
+                # retry if escaping could make a difference
+                return search_fn(escaped_q, **kw)
+            else:
+                raise
+    except (SolrError, OSError):
+        # fatal error
         log.exception('Error in solr search')
         if not ignore_errors:
-            match = re.search(r'<pre>(.*)</pre>', str(e))
-            raise SearchError('Error running search query: %s' %
-                              (match.group(1) if match else e))
+            raise SearchError('Error running search')
 
 
 def search_artifact(atype, q, history=False, rows=10, short_timeout=False, 
filter=None,
diff --git a/Allura/allura/tests/unit/test_solr.py 
b/Allura/allura/tests/unit/test_solr.py
index 6b5548df7..e25581ffd 100644
--- a/Allura/allura/tests/unit/test_solr.py
+++ b/Allura/allura/tests/unit/test_solr.py
@@ -189,6 +189,21 @@ def test_search_strips_local_params_before_pysolr(self, c, 
g):
         # pysolr.search must receive the query without the local-params prefix
         g.solr.search.assert_called_once_with('*:*')
 
+    @mock.patch('allura.lib.search.g')
+    @mock.patch('allura.lib.search.c')
+    def test_search_retries_with_escaped_query_on_solr_error(self, c, g):
+        # a query like 'git --log' is invalid Solr syntax; on the first
+        # SolrError, search() should retry with the escaped form
+        c.user.username = 'tester'
+        g.solr.search.side_effect = [SolrError('bad syntax'), 'ok result']
+
+        result = search('git --log')
+
+        assert result == 'ok result'
+        assert g.solr.search.call_count == 2
+        assert g.solr.search.call_args_list[0] == mock.call('git --log')
+        assert g.solr.search.call_args_list[1] == mock.call(r'git \-\-log')
+
 
 class TestSearch_app:
 

Reply via email to