Преглед изворни кода

Merge pull request #961 from kvch/user-visible-engine-errors

show engine errors in infobox && add new error alert
Adam Tauber пре 7 година
родитељ
комит
c30f73f8fd

+ 4
- 0
searx/results.py Прегледај датотеку

@@ -135,6 +135,7 @@ class ResultContainer(object):
135 135
         self._number_of_results = []
136 136
         self._ordered = False
137 137
         self.paging = False
138
+        self.unresponsive_engines = []
138 139
 
139 140
     def extend(self, engine_name, results):
140 141
         for result in list(results):
@@ -304,3 +305,6 @@ class ResultContainer(object):
304 305
         if not resultnum_sum or not self._number_of_results:
305 306
             return 0
306 307
         return resultnum_sum / len(self._number_of_results)
308
+
309
+    def add_unresponsive_engine(self, engine_name):
310
+        self.unresponsive_engines.append(engine_name)

+ 4
- 0
searx/search.py Прегледај датотеку

@@ -20,6 +20,7 @@ import sys
20 20
 import threading
21 21
 from time import time
22 22
 from uuid import uuid4
23
+from flask_babel import gettext
23 24
 import requests.exceptions
24 25
 import searx.poolrequests as requests_lib
25 26
 from searx.engines import (
@@ -133,18 +134,21 @@ def search_one_request_safe(engine_name, query, request_params, result_container
133 134
         requests_exception = False
134 135
 
135 136
         if (issubclass(e.__class__, requests.exceptions.Timeout)):
137
+            result_container.add_unresponsive_engine((engine_name, gettext('timeout')))
136 138
             # requests timeout (connect or read)
137 139
             logger.error("engine {0} : HTTP requests timeout"
138 140
                          "(search duration : {1} s, timeout: {2} s) : {3}"
139 141
                          .format(engine_name, search_duration, timeout_limit, e.__class__.__name__))
140 142
             requests_exception = True
141 143
         elif (issubclass(e.__class__, requests.exceptions.RequestException)):
144
+            result_container.add_unresponsive_engine((engine_name, gettext('request exception')))
142 145
             # other requests exception
143 146
             logger.exception("engine {0} : requests exception"
144 147
                              "(search duration : {1} s, timeout: {2} s) : {3}"
145 148
                              .format(engine_name, search_duration, timeout_limit, e))
146 149
             requests_exception = True
147 150
         else:
151
+            result_container.add_unresponsive_engine((engine_name, gettext('unexpected crash')))
148 152
             # others errors
149 153
             logger.exception('engine {0} : exception : {1}'.format(engine_name, e))
150 154
 

+ 12
- 4
searx/templates/oscar/messages/no_results.html Прегледај датотеку

@@ -1,9 +1,17 @@
1 1
 {% from 'oscar/macros.html' import icon %}
2
+{% if unresponsive_engines %}
3
+<div class="alert alert-danger fade in" role="alert">
4
+    <p><strong class="lead">{{ icon('remove-sign') }} {{ _('Error!') }}</strong> {{ _('Engines cannot retrieve results.') }}</p>
5
+    <p>
6
+        {% for engine_name, error_type in unresponsive_engines %}
7
+        {{ engine_name }} ({{ error_type }}){% if not loop.last %}, {% endif %}
8
+        {% endfor %}
9
+    </p>
10
+    <p><small>{{ _('Please, try again later or find another searx instance.') }}</small></p>
11
+</div>
12
+{% else %}
2 13
 <div class="alert alert-info fade in" role="alert">
3
-    <button class="close" data-dismiss="alert" type="button">
4
-        <span aria-hidden="true">×</span>
5
-        <span class="sr-only">{{ _('Close') }}</span>
6
-    </button>
7 14
     <strong class="lead">{{ icon('info-sign') }} {{ _('Sorry!') }}</strong>
8 15
     {{ _('we didn\'t find any results. Please use another query or search in more categories.') }}
9 16
 </div>
17
+{% endif %}

+ 10
- 0
searx/templates/oscar/results.html Прегледај датотеку

@@ -94,6 +94,16 @@
94 94
             {% if number_of_results != '0' %}
95 95
                 <p><small>{{ _('Number of results') }}: {{ number_of_results }}</small></p>
96 96
             {% endif %}
97
+
98
+            {% if unresponsive_engines and results|length >= 1 %}
99
+            <div class="alert alert-danger fade in" role="alert">
100
+                <p>{{ _('Engines cannot retrieve results') }}:</p>
101
+                {% for engine_name, error_type in unresponsive_engines %}
102
+                {{ engine_name }} ({{ error_type }}){% if not loop.last %}, {% endif %}
103
+                {% endfor %}
104
+            </div>
105
+            {% endif %}
106
+
97 107
             {% if infoboxes %}
98 108
                 {% for infobox in infoboxes %}
99 109
                     {% include 'oscar/infobox.html' %}

+ 3
- 1
searx/webapp.py Прегледај датотеку

@@ -534,7 +534,8 @@ def index():
534 534
                                     'answers': list(result_container.answers),
535 535
                                     'corrections': list(result_container.corrections),
536 536
                                     'infoboxes': result_container.infoboxes,
537
-                                    'suggestions': list(result_container.suggestions)}),
537
+                                    'suggestions': list(result_container.suggestions),
538
+                                    'unresponsive_engines': list(result_container.unresponsive_engines)}),
538 539
                         mimetype='application/json')
539 540
     elif output_format == 'csv':
540 541
         csv = UnicodeWriter(StringIO())
@@ -573,6 +574,7 @@ def index():
573 574
         corrections=result_container.corrections,
574 575
         infoboxes=result_container.infoboxes,
575 576
         paging=result_container.paging,
577
+        unresponsive_engines=result_container.unresponsive_engines,
576 578
         current_language=search_query.lang,
577 579
         base_url=get_base_url(),
578 580
         theme=get_current_theme_name(),

+ 1
- 0
tests/unit/test_webapp.py Прегледај датотеку

@@ -39,6 +39,7 @@ class ViewsTestCase(SearxTestCase):
39 39
                                                 corrections=set(),
40 40
                                                 suggestions=set(),
41 41
                                                 infoboxes=[],
42
+                                                unresponsive_engines=[],
42 43
                                                 results=self.test_results,
43 44
                                                 results_number=lambda: 3,
44 45
                                                 results_length=lambda: len(self.test_results))