# HG changeset patch # User Daniele Nicolodi # Date 1313943447 -7200 # Node ID 601d49a7c35372fb302e9c794c8d3a3e4ae50e02 # Parent 65c700a145c9a6080907fe05fb63ba9ad0e63950 Smarter way to register error handlers. diff -r 65c700a145c9 -r 601d49a7c353 src/ltpdarepo/__init__.py --- 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') diff -r 65c700a145c9 -r 601d49a7c353 src/ltpdarepo/templates/error.html --- 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 %} -

{{ error }}

+

{{ error.code }}: {{ error.name }}

+
{{ error.description|safe }}
{% endblock %}