Challenge - 5 Problems
Flask Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a 404 error occurs in this Flask app?
Consider this Flask app snippet that handles 404 errors. What will the user see if they visit a non-existent page?
Flask
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.route('/') def home(): return 'Welcome Home!' if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Look at the @app.errorhandler decorator and what it returns.
✗ Incorrect
The @app.errorhandler(404) decorator catches 404 errors and returns the custom '404.html' template with a 404 status code. This prevents the default Flask error page.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Flask error handler
Which option correctly fixes the syntax error in this Flask error handler code?
Flask
from flask import Flask app = Flask(__name__) @app.errorhandler(500) def internal_error(error): return 'Internal Server Error', 500
Attempts:
2 left
💡 Hint
Check the function definition line syntax.
✗ Incorrect
Python function definitions require a colon at the end of the def line. Missing colon causes a SyntaxError.
🔧 Debug
advanced2:00remaining
Why does this Flask app not show the custom 500 error page?
This Flask app tries to show a custom 500 error page but always shows the default Flask error page instead. What is the cause?
Flask
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler(500) def internal_error(e): return render_template('500.html'), 500 @app.route('/') def home(): 1/0 # force error if __name__ == '__main__': app.run(debug=True)
Attempts:
2 left
💡 Hint
Think about Flask's behavior when debug mode is on.
✗ Incorrect
When debug=True, Flask shows the interactive debugger on errors instead of calling error handlers. To see custom error pages, debug must be off.
❓ state_output
advanced2:00remaining
What is the HTTP status code returned by this Flask error handler?
Given this Flask error handler, what status code will the client receive?
Flask
from flask import Flask, abort app = Flask(__name__) @app.errorhandler(403) def forbidden(e): return 'Access Denied', 401 @app.route('/secret') def secret(): abort(403) if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Look at the return statement inside the error handler.
✗ Incorrect
The error handler for 403 returns a response with status code 401, so the client receives 401 despite the error being 403.
🧠 Conceptual
expert2:00remaining
Why should you avoid showing detailed error messages in production Flask apps?
Which option best explains why detailed error messages should be hidden in production Flask applications?
Attempts:
2 left
💡 Hint
Think about security risks of exposing internal details.
✗ Incorrect
Showing detailed errors can expose code, server paths, or database info that attackers can use to find vulnerabilities. Production apps should show generic messages.