changeset 95:601d49a7c353

Smarter way to register error handlers.
author Daniele Nicolodi <daniele@grinta.net>
date Sun, 21 Aug 2011 18:17:27 +0200
parents 65c700a145c9
children e7e0258660aa
files src/ltpdarepo/__init__.py src/ltpdarepo/templates/error.html
diffstat 2 files changed, 12 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/__init__.py	Sun Aug 21 18:17:27 2011 +0200
+++ b/src/ltpdarepo/__init__.py	Sun Aug 21 18:17:27 2011 +0200
@@ -1,9 +1,12 @@
 from urlparse import urlparse, urljoin
+
 from flask import Flask, g, request, session, render_template, Markup, redirect, flash, url_for
+from werkzeug.exceptions import InternalServerError
+
 from pkg_resources import get_distribution
 import MySQLdb as mysql
 
-from .security import secure, require, authenticate
+from ltpdarepo.security import secure, require, authenticate
 
 
 SCHEMA = 2.7
@@ -37,16 +40,12 @@
     g.db.close()
 
 
-# non authorized error handler
-@app.errorhandler(403)
-def non_authorized(error):
-    return render_template('error.html', error=error), 403
-
-
-# not found error handler
-@app.errorhandler(404)
-def not_found(error):
-    return render_template('error.html', error=error), 404
+# register error handlers
+from werkzeug import exceptions
+def error_handler(error):
+    return render_template('error.html', error=error), error.code
+for exc in exceptions.default_exceptions:
+    app.error_handler_spec[None][exc] = error_handler
 
 
 @app.template_filter('breadcrumbs')
--- a/src/ltpdarepo/templates/error.html	Sun Aug 21 18:17:27 2011 +0200
+++ b/src/ltpdarepo/templates/error.html	Sun Aug 21 18:17:27 2011 +0200
@@ -1,5 +1,6 @@
 {% extends "layout.html" %}
 {% block title %}{{ error }}{% endblock %}
 {% block body %}
-  <h2 class="error">{{ error }}</h2>
+  <h2 class="error">{{ error.code }}: {{ error.name }}</h2>
+  <div class="discrete">{{ error.description|safe }}</div>
 {% endblock %}