0
0
Flaskframework~10 mins

Custom error pages (404, 500) in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom error pages (404, 500)
User requests URL
Flask checks route
Yes
Route found -> Return page
No
Error triggered (404 or 500)
Custom error handler catches error
Render custom error page
Send error page to user
When a user visits a URL, Flask checks if the route exists. If not, or if an error occurs, Flask triggers an error. Custom error handlers catch these errors and show friendly error pages.
Execution Sample
Flask
from flask import Flask, render_template
app = Flask(__name__)

@app.errorhandler(404)
def not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_error(e):
    return render_template('500.html'), 500
This code sets custom pages for 404 and 500 errors by telling Flask to use the 404.html and 500.html templates when a page is not found or an internal error occurs.
Execution Table
StepUser RequestRoute Found?Error TriggeredHandler CalledPage RenderedResponse Code
1/homeYesNoNoHome page200
2/unknownNoYes (404)Yes (404 handler)Custom 404 page404
3/cause-errorYesYes (500)Yes (500 handler)Custom 500 page500
4End-----
💡 Execution stops after sending the response page to the user.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
user_requestNone/home/unknown/cause-errorNone
route_foundFalseTrueFalseTrueN/A
error_triggeredNoneNo404500N/A
handler_calledNoneNo404 handler500 handlerN/A
page_renderedNoneHome pageCustom 404 pageCustom 500 pageN/A
response_codeNone200404500N/A
Key Moments - 2 Insights
Why does Flask call the 404 error handler instead of returning a normal page?
Because the route was not found (see execution_table row 2), Flask triggers a 404 error and calls the custom 404 handler to show a friendly page.
How does Flask know to use the custom 500 error page?
When an internal error happens (row 3), Flask triggers a 500 error and calls the registered 500 error handler to render the custom page.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response code when the user requests '/unknown'?
A200
B500
C404
DNone
💡 Hint
Check the 'Response Code' column for the row where 'User Request' is '/unknown'.
At which step does Flask call the 500 error handler?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Handler Called' column to find when the 500 handler is used.
If the route '/home' did not exist, what would change in the execution table?
AStep 1 would show error triggered as 404 and handler called as 404 handler
BStep 1 would show error triggered as 500 and handler called as 500 handler
CStep 1 would remain the same with response code 200
DStep 1 would be removed
💡 Hint
Refer to how missing routes trigger 404 errors in the execution_table rows.
Concept Snapshot
Custom error pages in Flask:
- Use @app.errorhandler(code) decorator
- Define a function returning a template and status code
- Flask calls handler on matching error (404, 500)
- Shows friendly error pages instead of default messages
- Helps users understand errors clearly
Full Transcript
When a user visits a website built with Flask, the app checks if the requested page exists. If it does, Flask shows the page normally with a 200 status code. If the page is missing, Flask triggers a 404 error. If something goes wrong inside the app, it triggers a 500 error. Developers can write special functions called error handlers to catch these errors. These handlers return custom pages like 'Page Not Found' or 'Server Error' to help users understand what happened. This makes the website friendlier and easier to use. The code example shows how to create a custom 404 page by using the @app.errorhandler decorator and returning a template with the 404 status code.