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

Fill in your details below or click an icon to log in: 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