Writing nice and shiny error messages and assigning them to the application's error handlers can be a lot of work. If you're happy with the default descriptions which are provided by werkzeug, you're done with a few lines of code:
from flask import Markup, render_template
from werkzeug.exceptions import default_exceptions
def show_errormessage(error):
desc = error.get_description(flask.request.environ)
return render_template('error.html',
code=error.code,
name=error.name,
description=Markup(desc)
), error.code
for _exc in default_exceptions:
app.error_handlers[_exc] = show_errormessage
del _exc
And here the example template:
{% extends "base.html" %}
{% block title %}Error {{ code }}: {{ name }}{% endblock %}
{% block body %}
{{ description}}
{% endblock %}
Make sure to either wrap the description with Markup() or to use the |safe filter in the template on the description as it contains HTML.
This snippet by Bernd Zeimetz can be used freely for anything you like. Consider it public domain.
Comments
Updated Snippet by Armin Ronacher on 2010-05-15 @ 21:32
Hi, I modified the snippet slightly to use
Markup()and I also got rid of the map call, a loop is nicer here.Bug with default handling of uncaught exceptions by Pavel Repin on 2012-01-28 @ 00:31
After looking at source code of Flask.handle_exception(), I am pretty sure the above code will break, because 'show_error_message' may be called 'error' value that's not a Werkzeug HTTPException but a thing like ZeroDivisionError or whatever, which lacks attributes like 'code', 'name', etc.
See: https://github.com/mitsuhiko/flask/blob/92dbe3153a9e0c007ecd1987ccd5f3d796c901af/flask/app.py#L1196