📖 Scenario: You are building a simple Flask web app that needs to handle errors gracefully. Instead of showing default error pages, you want to create custom error messages for common HTTP errors like 404 (Not Found) and 500 (Internal Server Error).
🎯 Goal: Build a Flask app that uses error handler decorators to catch 404 and 500 errors and return custom messages.
📋 What You'll Learn
Create a Flask app instance named app
Use the @app.errorhandler(404) decorator to handle 404 errors
Use the @app.errorhandler(500) decorator to handle 500 errors
Return a simple string message for each error handler function
💡 Why This Matters
🌍 Real World
Web apps need to handle errors gracefully to improve user experience and provide helpful feedback when something goes wrong.
💼 Career
Knowing how to use Flask error handler decorators is important for backend web developers to create robust and user-friendly applications.
Progress0 / 4 steps
1
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Add a 404 error handler decorator
Use the @app.errorhandler(404) decorator to create a function called handle_404 that returns the string 'Page not found'.
Flask
Hint
The function must accept one parameter, usually named error.
3
Add a 500 error handler decorator
Use the @app.errorhandler(500) decorator to create a function called handle_500 that returns the string 'Internal server error'.
Flask
Hint
Like the 404 handler, the function must accept one parameter error.
4
Add a simple route and run the app
Add a route for '/' that returns 'Welcome to the homepage'. Then add the if __name__ == '__main__' block to run the app with app.run().
Flask
Hint
The route function should be named home and return the welcome string.
Practice
(1/5)
1. What is the main purpose of using @app.errorhandler decorator in Flask?
easy
A. To catch specific errors and return custom responses
B. To define routes for the application
C. To start the Flask server
D. To connect to a database
Solution
Step 1: Understand the role of error handlers
Error handlers catch errors like 404 or 500 and let you respond with custom messages.
Step 2: Recognize the decorator's purpose
@app.errorhandler is used to register functions that handle specific errors gracefully.
Final Answer:
To catch specific errors and return custom responses -> Option A
Quick Check:
Error handler = Custom error response [OK]
Hint: Error handlers catch errors and customize replies [OK]
Common Mistakes:
Confusing errorhandler with route decorator
Thinking it starts the server
Assuming it manages database connections
2. Which of the following is the correct syntax to handle 404 errors in Flask?
easy
A. @app.errorhandler('404')
def handle_404(error):
return 'Not Found', 404
B. @app.errorhandler(404)
def handle_404(error):
return 'Not Found', 404
C. @app.route(404)
def handle_404():
return 'Not Found'
D. @app.errorhandler(HTTP_404)
def handle_404(error):
return 'Not Found', 404
Solution
Step 1: Identify correct decorator usage
The @app.errorhandler decorator takes an integer error code like 404, not a string or undefined constant.
Step 2: Check function signature and return
The handler function must accept an error argument and return a response with status code.
Final Answer:
@app.errorhandler(404)
def handle_404(error):
return 'Not Found', 404 -> Option B
Quick Check:
Use integer error code with errorhandler [OK]
Hint: Use integer error code, not string or constants [OK]
Common Mistakes:
Using @app.route instead of @app.errorhandler
Passing error code as string '404'
Using undefined constants like HTTP_404
3. What will be the output when a 500 error occurs with this code?