Learning Flask: Part 4: Errors

Learning Flask

Flask is a microframework for server-side web development in Python.


Errors

In Flask, routes normally work with render_template() to react according to HTTP status codes. That is, a normal response is 200 and errors happen in 4XX and 5XX range.

Because 200 is the default, it is not needed for a normal response. However, the most correct response would be to return the code “200” along with any textual data.

Handling Errors

Along with route(), there is also the errorhandler() function. This takes as a parameter the status code or the name of a known exception.

In order to pass back the error, the code should also be returned. This allows the server to correctly register the error to the user.

However, the render_template() can also be used to deliver specific error files or templates along with any other reactions.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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.