[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
This commit is contained in:
Adam Tauber 2016-11-14 22:07:23 +01:00
parent 88dfee858e
commit 832cf37a97
2 changed files with 25 additions and 6 deletions

View file

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

View file

@ -344,6 +344,8 @@ def render(template_name, override_theme=None, **kwargs):
kwargs['cookies'] = request.cookies kwargs['cookies'] = request.cookies
kwargs['errors'] = request.errors
kwargs['instance_name'] = settings['general']['instance_name'] kwargs['instance_name'] = settings['general']['instance_name']
kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab') 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):
@app.before_request @app.before_request
def pre_request(): def pre_request():
# merge GET, POST vars request.errors = []
preferences = Preferences(themes, categories.keys(), engines, plugins) preferences = Preferences(themes, categories.keys(), engines, plugins)
try: try:
preferences.parse_cookies(request.cookies) preferences.parse_cookies(request.cookies)
except: except:
# TODO throw error message to the user request.errors.append(gettext('Invalid settings, please edit your preferences'))
logger.warning('Invalid config')
request.preferences = preferences request.preferences = preferences
# merge GET, POST vars
# request.form # request.form
request.form = dict(request.form.items()) request.form = dict(request.form.items())
for k, v in request.args.items(): for k, v in request.args.items():
@ -397,7 +400,7 @@ def index():
Supported outputs: html, json, csv, rss. Supported outputs: html, json, csv, rss.
""" """
if not request.args and not request.form: if request.form.get('q') is None:
return render( return render(
'index.html', 'index.html',
) )
@ -410,7 +413,8 @@ def index():
# search = Search(search_query) # without plugins # search = Search(search_query) # without plugins
search = SearchWithPlugins(search_query, request) search = SearchWithPlugins(search_query, request)
result_container = search.search() result_container = search.search()
except Exception: except:
request.errors.append(gettext('search error'))
logger.exception('search error') logger.exception('search error')
return render( return render(
'index.html', 'index.html',
@ -573,7 +577,7 @@ def preferences():
try: try:
request.preferences.parse_form(request.form) request.preferences.parse_form(request.form)
except ValidationException: except ValidationException:
# TODO use flash feature of flask request.errors.append(gettext('Invalid settings, please edit your preferences'))
return resp return resp
return request.preferences.save(resp) return request.preferences.save(resp)