Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Flask Error Handling – Display Custom Error Pages in Flask

Flask Error Handling

This article will deal with different types of  HTTP errors and then learn how to use Flask error handling to tackle these errors. So let’s get started!

Why do we Need Error Handling?

An error in a web application can happen due to several reasons. It can be due to an incorrect code in the App or some bad requests by the user or server downtime.

Hence it is critical to handle these errors. The browsers, though by-default handles HTTP errors for you, the output is not quite aesthetic. 

For example, while building a Flask application, you might have come across 500 internal server error.

500 Internal Server Error
500 Internal Server Error

A simple line indicating the reason for the error would have sufficed, instead of displaying irrelevant data.

This is where Flask Error handlers come into the picture.

With Flask error Handlers, we can:

  1. Customize the error page look.
  2. Show only relevant data to the user.

Common HTTP errors

Some of the most common errors raised are:

HTTP Error CodesMeaning
301Moved Permanently
302Moved Temporarily
400Bad Request
403Forbidden
404Not Found
429Too Many Requests
500Internal Server Error
502Bad Gateway
503Service Unavailable
504Gateway Timeout
HTTP Errors

Hands-On with Flask Error Handling

Error codes – 404 and 500 are the most common errors that we deal with every day.

So in this section we will build a simple Error handler for 404 and 500. The syntax for other errors will be exactly the same.

In flask, we use the built-in error_handler decorator.

The syntax is:

@app.errorhandler(status_code)
def function_name(error):
    return render_template('xyz.html'),status_code

Hence consider the following Flask application example:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/blogs')
def blog():
    return render_template('blog.html')

#Handling error 404 and displaying relevant web page
@app.errorhandler(404)
def not_found_error(error):
    return render_template('404.html'),404

#Handling error 500 and displaying relevant web page
@app.errorhandler(500)
def internal_error(error):
    return render_template('500.html'),500

#app.run(host='localhost', port=5000)
app.run(host='localhost', port=5000)

The Blog.html:

<h2>Welcome to the Blog</h2>

The 404.html file:

<h2>The webpage you are trying is not found</h2>
<img src = "{{url_for('static','images/opps.jpg') }}"

Here we are using a image to show on the 404 web page as well

Similarly the 500.html file:

<h2>Something Went Wrong</h2>

Implementation

Now run the server and go to any arbitary non-existant URL endpoint

404
404

Now to get the 500 error, deliberately interchange a few letters of render_template() to let’s say remder_template()

Now restart the server and go to “/blogs” URL. You will now get the 500 error page

500 Error page
500 Error Page

Perfect!

Conclusion

That’s it, guys !! You can now customize the error pages as well based on your webpage theme. Do check out our other Flask tutorials to know more about Flask.

See you guys in the next article !! Happy Coding 🙂