Python Flask error handlers

Writing general error handlers for Python Flask applications is very straightdorward

def sqlite3_op_error(err):
 return render_template('errors/500.html', errmsg=('Database error: %s' % err))
def peewee_op_error(err):
 return render_template('errors/500.html', errmsg=('ORM error: %s' % err))
def name_error(err):
 return render_template('errors/500.html', errmsg=('Application error: %s' % err))
def page_not_found(err):
 return render_template('errors/404.html', errmsg=(err))
def query_does_not_exist(err):
 return render_template('errors/409.html', errmsg=('Query error: %s' % err))
def query_integrity_error(err):
 return render_template('errors/409.html', errmsg=('Integrify error: %s' % err))

A template might take the form

{%- extends "error_layout.html" -%}
{%- block fourzeronine -%}
{{ super() }}

A database query error has occurred: {{ errmsg }}
<p>Back to <a href="/">index</a>.
{%- endblock -%}


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.