Преглед на файлове

[enh] display errors

also tried flask's flash feature but flask creates session cookies if it
isn't flushed. Avoiding session cookies to preserve privacy
Adam Tauber преди 8 години
родител
ревизия
832cf37a97
променени са 2 файла, в които са добавени 25 реда и са изтрити 6 реда
  1. 15
    0
      searx/templates/oscar/base.html
  2. 10
    6
      searx/webapp.py

+ 15
- 0
searx/templates/oscar/base.html Целия файл

@@ -1,3 +1,4 @@
1
+{% from 'oscar/macros.html' import icon %}
1 2
 <!DOCTYPE html>
2 3
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"{% if rtl %} dir="rtl"{% endif %}>
3 4
 <head>
@@ -54,6 +55,20 @@
54 55
 <body>
55 56
     {% include 'oscar/navbar.html' %}
56 57
     <div class="container">
58
+    {% if errors %}
59
+        <div class="alert alert-danger fade in" role="alert">
60
+            <button class="close" data-dismiss="alert" type="button">
61
+                <span aria-hidden="true">×</span>
62
+                <span class="sr-only">{{ _('Close') }}</span>
63
+            </button>
64
+            <strong class="lead">{{ icon('info-sign') }} {{ _('Error!') }}</strong>
65
+            <ul>
66
+            {% for message in errors %}
67
+                <li>{{ message }}</li>
68
+            {% endfor %}
69
+            </ul>
70
+        </div>
71
+    {% endif %}
57 72
 
58 73
     {% block site_alert_error %}
59 74
     {% endblock %}

+ 10
- 6
searx/webapp.py Целия файл

@@ -344,6 +344,8 @@ def render(template_name, override_theme=None, **kwargs):
344 344
 
345 345
     kwargs['cookies'] = request.cookies
346 346
 
347
+    kwargs['errors'] = request.errors
348
+
347 349
     kwargs['instance_name'] = settings['general']['instance_name']
348 350
 
349 351
     kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')
@@ -364,15 +366,16 @@ def render(template_name, override_theme=None, **kwargs):
364 366
 
365 367
 @app.before_request
366 368
 def pre_request():
367
-    # merge GET, POST vars
369
+    request.errors = []
370
+
368 371
     preferences = Preferences(themes, categories.keys(), engines, plugins)
369 372
     try:
370 373
         preferences.parse_cookies(request.cookies)
371 374
     except:
372
-        # TODO throw error message to the user
373
-        logger.warning('Invalid config')
375
+        request.errors.append(gettext('Invalid settings, please edit your preferences'))
374 376
     request.preferences = preferences
375 377
 
378
+    # merge GET, POST vars
376 379
     # request.form
377 380
     request.form = dict(request.form.items())
378 381
     for k, v in request.args.items():
@@ -397,7 +400,7 @@ def index():
397 400
     Supported outputs: html, json, csv, rss.
398 401
     """
399 402
 
400
-    if not request.args and not request.form:
403
+    if request.form.get('q') is None:
401 404
         return render(
402 405
             'index.html',
403 406
         )
@@ -410,7 +413,8 @@ def index():
410 413
         # search = Search(search_query) #  without plugins
411 414
         search = SearchWithPlugins(search_query, request)
412 415
         result_container = search.search()
413
-    except Exception:
416
+    except:
417
+        request.errors.append(gettext('search error'))
414 418
         logger.exception('search error')
415 419
         return render(
416 420
             'index.html',
@@ -573,7 +577,7 @@ def preferences():
573 577
         try:
574 578
             request.preferences.parse_form(request.form)
575 579
         except ValidationException:
576
-            # TODO use flash feature of flask
580
+            request.errors.append(gettext('Invalid settings, please edit your preferences'))
577 581
             return resp
578 582
         return request.preferences.save(resp)
579 583