Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Nicer Errors with Descriptions from Werkzeug

Posted by Bernd Zeimetz on 2010-05-08 @ 12:42 and filed in Utilities

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',
    ), 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.