changeset 140:3a56d5501ffa

Add HTTP error handling
author Benoît Allard <benoit.allard@greenbone.net>
date Mon, 27 Oct 2014 16:46:18 +0100
parents 584587a000a2
children ce39a5267998
files farol/main.py farol/templates/base.j2 farol/templates/error.j2 farol/templates/layout.j2
diffstat 4 files changed, 193 insertions(+), 124 deletions(-) [+]
line wrap: on
line diff
--- a/farol/main.py	Mon Oct 27 15:08:29 2014 +0100
+++ b/farol/main.py	Mon Oct 27 16:46:18 2014 +0100
@@ -87,6 +87,13 @@
 def makeId(string):
     return secure_filename(string)
 
+@app.errorhandler(400)
+@app.errorhandler(404)
+@app.errorhandler(405)
+@app.errorhandler(500)
+def error_page(error):
+    return render_template('error.j2', e=error), getattr(error, 'code', 500)
+
 @app.route('/')
 def welcome():
     return render_template('welcome.j2',
--- a/farol/templates/base.j2	Mon Oct 27 15:08:29 2014 +0100
+++ b/farol/templates/base.j2	Mon Oct 27 16:46:18 2014 +0100
@@ -23,107 +23,83 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 -#}
 
+{% extends "layout.j2" %}
+
 {% from "macros.j2" import modal, POST_button -%}
 
-<!doctype html>
+{% block navbar %}
+  {% if has_current %}
+    <li{% if active == 'document' %} class="active"{% endif %}><a href="{{ url_for('document.view') }}">Document</a></li>
+  {% endif %}
+  {% if products %}
+    <li class="dropdown{{ ' active' if active == 'product' }}">
+      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Products <span class="caret"></span></a>
+      <ul class="dropdown-menu" role="menu">
+        <li role="presentation">
+          <a role="menuitem" tabindex="-1" href="{{ url_for('producttree.view') }}">View Product Tree</a>
+        </li>
+        <li role="presentation" class="divider"></li>
+        {% for name, productid in products | sort %}
+          <li><a href="{{ url_for('producttree.view_product', productid=productid) }}">{{ name }}</a></li>
+        {% endfor %}
+      </ul>
+    </li>
+  {% endif %}
+  {% if vulnerabilities %}
+    <li class="dropdown{{ ' active' if active == 'vulnerability' }}">
+      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Vulnerabilities <span class="caret"></span></a>
+      <ul class="dropdown-menu" role="menu">
+        {% for name, ord in vulnerabilities %}
+          <li><a href="{{ url_for('vulnerability.view', ordinal=ord) }}">{{ name }}</a></li>
+        {% endfor %}
+      </ul>
+    </li>
+  {% endif %}
+  {% if has_current %}
+    <li>
+      {% if error %}
+        <p class="navbar-text">Document is <a id="error-popover" href="#" tabindex="0" class="navbar-link" data-toggle="popover" data-trigger="focus" data-placement="bottom" title="First Error:" data-content="{{ error }}"><strong>invalid</strong></a> <span class="badge progress-bar-danger"><strong>&#x2717;</strong></span></p>
+      {% else %}
+        <p class="navbar-text">Document looks valid <span class="badge progress-bar-success"><strong>&#x2713;</strong></span></p>
+      {% endif %}
+    </li>
+  {% endif %}
+{% endblock %}
 
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>Farol - {% block title %}{% endblock %}</title>
-  <link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.css') }}">
-  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
-</head>
-<body>
-  <script src="{{ url_for('static', filename='jquery-2.1.1.min.js') }}"></script>
-  <script src="{{ url_for('static', filename='bootstrap.js') }}"></script>
-  <nav class="navbar navbar-inverse" role="navigation">
-    <div class="container">
-      {# Brand and toggle get grouped for better mobile display #}
-      <div class="navbar-header">
-        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
-          <span class="sr-only">Toggle navigation</span>
-          <span class="icon-bar"></span>
-          <span class="icon-bar"></span>
-          <span class="icon-bar"></span>
-        </button>
-        <a class="navbar-brand" href="{{ url_for('welcome') }}" title="A Security Advisory Management Platform">Farol</a>
-      </div>
-
-      {# Collect the nav links, forms, and other content for toggling #}
-      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
-        <ul class="nav navbar-nav">
-          <li{% if active == 'new' %} class="active"{% endif %}><a href="{{ url_for('new') }}">New</a></li>
-          {% if has_current %}
-          <li{% if active == 'document' %} class="active"{% endif %}><a href="{{ url_for('document.view') }}">Document</a></li>
-          {% endif %}
-          {% if products %}
-          <li class="dropdown{{ ' active' if active == 'product' }}">
-            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Products <span class="caret"></span></a>
-            <ul class="dropdown-menu" role="menu">
-              <li role="presentation">
-                <a role="menuitem" tabindex="-1" href="{{ url_for('producttree.view') }}">View Product Tree</a>
-              </li>
-              <li role="presentation" class="divider"></li>
-              {% for name, productid in products | sort %}
-                <li><a href="{{ url_for('producttree.view_product', productid=productid) }}">{{ name }}</a></li>
-              {% endfor %}
-            </ul>
+{% block navbar_right %}
+  {% if has_current %}
+    <li class="dropdown{{ ' active' if active == 'render' }}">
+      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Export <span class="caret"></span></a>
+      <ul class="dropdown-menu" role="menu">
+        {% for format in ('cvrf', 'nasl', 'oval') %}<li><a href="{{ url_for('render', format_=format)}}">as {{ format | upper }}</a></li>{% endfor %}
+      </ul>
+    </li>
+  {% endif %}
+  {% if caching %}
+    <li class="dropdown">
+      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Cache <span class="caret"></span></a>
+      <ul class="dropdown-menu" role="menu">
+        <li role="presentation"{{ ' class="disabled"' if not has_current }}>
+          <a role="menuitem" tabindex="-1" href="{{ url_for('cache.save') }}">Save {{ current_id }}</a>
+        </li>
+        <li role="presentation" class="divider"></li>
+        {% for element in cache | sort %}
+          <li role="presentation">
+            {% if has_current %}
+              <a href="#{{element}}_modal" data-toggle="modal">Load {{ element }}</a>
+            {% else %}
+              {% call(selector) POST_button(url_for('cache.load', element=element), out=True) %}
+                <a role="menuitem" href="#" onclick="{{ selector }}.submit();return false;">Load {{ element }}</a>
+              {% endcall %}
+            {% endif %}
           </li>
-          {% endif %}
-          {% if vulnerabilities %}
-          <li class="dropdown{{ ' active' if active == 'vulnerability' }}">
-            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Vulnerabilities <span class="caret"></span></a>
-            <ul class="dropdown-menu" role="menu">
-              {% for name, ord in vulnerabilities %}
-                <li><a href="{{ url_for('vulnerability.view', ordinal=ord) }}">{{ name }}</a></li>
-              {% endfor %}
-            </ul>
-          </li>
-          {% endif %}
-        </ul>
-        {% if has_current %}
-          {% if error %}
-            <p class="navbar-text">Document is <a id="error-popover" href="#" tabindex="0" class="navbar-link" data-toggle="popover" data-trigger="focus" data-placement="bottom" title="First Error:" data-content="{{ error }}"><strong>invalid</strong></a> <span class="badge progress-bar-danger"><strong>&#x2717;</strong></span></p>
-          {% else %}
-            <p class="navbar-text">Document looks valid <span class="badge progress-bar-success"><strong>&#x2713;</strong></span></p>
-          {% endif %}
-        {% endif %}
-        <ul class="nav navbar-nav navbar-right">
-          {% if has_current %}
-          <li class="dropdown{{ ' active' if active == 'render' }}">
-            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Export <span class="caret"></span></a>
-            <ul class="dropdown-menu" role="menu">
-              {% for format in ('cvrf', 'nasl', 'oval') %}<li><a href="{{ url_for('render', format_=format)}}">as {{ format | upper }}</a></li>{% endfor %}
-            </ul>
-          </li>
-          {% endif %}
-          {% if caching %}
-          <li class="dropdown">
-            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Cache <span class="caret"></span></a>
-            <ul class="dropdown-menu" role="menu">
-              <li role="presentation"{{ ' class="disabled"' if not has_current }}>
-                <a role="menuitem" tabindex="-1" href="{{ url_for('cache.save') }}">Save {{ current_id }}</a>
-              </li>
-              <li role="presentation" class="divider"></li>
-              {% for element in cache | sort %}
-                <li role="presentation">
-                  {% if has_current %}
-                    <a href="#{{element}}_modal" data-toggle="modal">Load {{ element }}</a>
-                  {% else %}
-                    {% call(selector) POST_button(url_for('cache.load', element=element), out=True) %}
-                      <a role="menuitem" href="#" onclick="{{ selector }}.submit();return false;">Load {{ element }}</a>
-                    {% endcall %}
-                  {% endif %}
-                </li>
-              {% endfor %}
-            </ul>
-          </li>
-          {% endif %}
-        </ul>
-      </div>{# /.navbar-collapse #}
-    </div>{# /.container-fluid #}
-  </nav>
+        {% endfor %}
+      </ul>
+    </li>
+  {% endif %}
+{% endblock %}
+
+{% block pre_content %}
   {% if has_current %}
     {% for element in cache %}
       {# Put the modals for the load action here #}
@@ -137,32 +113,20 @@
       {% endcall %}
     {% endfor %}
   {% endif %}
-  <div class="main container">
-    {% with messages = get_flashed_messages(with_categories=True) %}
-      {% if messages %}
-      <div class="flashes">
-      {% for category, message in messages %}
-        {% if category == 'message' %}{% set category = "info" %}{% endif %}
-        <div class="alert alert-{{ category }}">{{ message }}</div>
-      {% endfor %}
-      </div>
-      {% endif %}
-    {% endwith %}
-    <div>
-      <script>$("#error-popover").popover();</script>
-      {% block content %}{% endblock %}
+  {% with messages = get_flashed_messages(with_categories=True) %}
+    {% if messages %}
+    <div class="flashes">
+    {% for category, message in messages %}
+      {% if category == 'message' %}{% set category = "info" %}{% endif %}
+      <div class="alert alert-{{ category }}">{{ message }}</div>
+    {% endfor %}
     </div>
-    {% if config.DEBUG and not config.DEBUG_SURE %}
-    <div class="alert alert-danger"><strong>DEBUG:</strong> This application is running in debug mode. See the <a href="{{ url_for('about') }}#debug">about page</a> for more Details</div>
     {% endif %}
-  </div>
-  <footer class="footer container-fluid navbar-inverse">
-    <div class="text-center">
-      <span class="text-muted">Copyright &copy; 2014 Greenbone Networks GmbH</span>
-      |
-      <span><a href="{{ url_for('about') }}">About Farol</a></span>
-    </div>
-    <a href="http://greenbone.net/" id="greenbone" class="logo_img text-hide center-block">Greenbone Networks GmbH</a>
-  </footer>
-</body>
-</html>
+  {% endwith %}
+{% endblock %}
+
+{% block post_content %}
+  {% if config.DEBUG and not config.DEBUG_SURE %}
+    <div class="alert alert-danger"><strong>DEBUG:</strong> This application is running in debug mode. See the <a href="{{ url_for('about') }}#debug">about page</a> for more Details</div>
+  {% endif %}
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/farol/templates/error.j2	Mon Oct 27 16:46:18 2014 +0100
@@ -0,0 +1,15 @@
+{% extends "layout.j2" %}
+
+{% block title %}{{ e }}{% endblock %}
+
+{% block content %}
+<div class="page-header">
+  <h1>{{ e }}</h1>
+</div>
+{% if e.description %}<p>{{ e.description }}</p><hr>{% endif %}
+{% if e.code != 404 %}
+  <p>Software are not without bugs. Looks like, you found one ... A trace has been written to the logs.</p>
+  <p>If you keep coming to this page, you might want to delete your document, and start again with a fresh one ...</p>
+  <div class="pull-right">{{ POST_button(url_for('document.delete'), text="Delete document", style="btn-danger") }}</div>
+{% endif %}
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/farol/templates/layout.j2	Mon Oct 27 16:46:18 2014 +0100
@@ -0,0 +1,83 @@
+{#
+# Description:
+# Web Template used in Farol Design
+#
+# Authors:
+# BenoƮt Allard <benoit.allard@greenbone.net>
+#
+# Copyright:
+# Copyright (C) 2014 Greenbone Networks GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+-#}
+
+{% from "macros.j2" import modal, POST_button -%}
+
+<!doctype html>
+
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>Farol - {% block title %}{% endblock %}</title>
+  <link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.css') }}">
+  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
+</head>
+<body>
+  <script src="{{ url_for('static', filename='jquery-2.1.1.min.js') }}"></script>
+  <script src="{{ url_for('static', filename='bootstrap.js') }}"></script>
+  <nav class="navbar navbar-inverse" role="navigation">
+    <div class="container">
+      {# Brand and toggle get grouped for better mobile display #}
+      <div class="navbar-header">
+        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
+          <span class="sr-only">Toggle navigation</span>
+          <span class="icon-bar"></span>
+          <span class="icon-bar"></span>
+          <span class="icon-bar"></span>
+        </button>
+        <a class="navbar-brand" href="{{ url_for('welcome') }}" title="A Security Advisory Management Platform">Farol</a>
+      </div>
+
+      {# Collect the nav links, forms, and other content for toggling #}
+      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
+        <ul class="nav navbar-nav">
+          <li{% if active == 'new' %} class="active"{% endif %}><a href="{{ url_for('new') }}">New</a></li>
+          {% block navbar %}
+          {% endblock %}
+        </ul>
+        <ul class="nav navbar-nav navbar-right">
+          {% block navbar_right %}
+          {% endblock %}
+        </ul>
+      </div>{# /.navbar-collapse #}
+    </div>{# /.container-fluid #}
+  </nav>
+  <div class="main container">
+    {% block pre_content %}{% endblock %}
+    <div>
+      {% block content %}{% endblock %}
+    </div>
+    {% block post_content %}{% endblock %}
+  </div>
+  <footer class="footer container-fluid navbar-inverse">
+    <div class="text-center">
+      <span class="text-muted">Copyright &copy; 2014 Greenbone Networks GmbH</span>
+      |
+      <span><a href="{{ url_for('about') }}">About Farol</a></span>
+    </div>
+    <a href="http://greenbone.net/" id="greenbone" class="logo_img text-hide center-block">Greenbone Networks GmbH</a>
+  </footer>
+</body>
+</html>

http://farol.wald.intevation.org