Challenge - 5 Problems
Flask Error Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a 404 error occurs?
Given this Flask app snippet, what response will the client receive when accessing a non-existent route?
Flask
from flask import Flask, jsonify app = Flask(__name__) @app.errorhandler(404) def not_found(error): return jsonify({'error': 'Not found'}), 404 @app.route('/') def home(): return 'Welcome!' # Client requests /missing
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 a JSON response with the error message and status code 404.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a 500 error handler?
Choose the correct Flask error handler syntax for HTTP 500 errors.
Attempts:
2 left
💡 Hint
Check the decorator syntax and function parameters.
✗ Incorrect
The correct syntax uses @app.errorhandler(500) without colon and the handler function must accept the error argument.
🔧 Debug
advanced2:00remaining
Why does this 403 error handler not work as expected?
Identify the issue in this Flask error handler code that prevents it from catching 403 errors.
Flask
from flask import Flask, abort app = Flask(__name__) @app.errorhandler(403) def forbidden(): return 'Forbidden', 403 @app.route('/secret') def secret(): abort(403)
Attempts:
2 left
💡 Hint
Check the function signature of the error handler.
✗ Incorrect
Error handler functions must accept the error argument to work properly. Missing it causes Flask to ignore the handler.
❓ state_output
advanced2:00remaining
What is the response status code after raising a custom exception handled by an error handler?
Consider this Flask app code. What status code will the client receive when '/fail' is accessed?
Flask
from flask import Flask, jsonify app = Flask(__name__) class CustomError(Exception): pass @app.errorhandler(CustomError) def handle_custom_error(e): return jsonify({'message': 'Custom error occurred'}), 418 @app.route('/fail') def fail(): raise CustomError()
Attempts:
2 left
💡 Hint
Look at the status code returned by the error handler.
✗ Incorrect
The error handler returns status code 418, so the client receives that code when the exception is raised.
🧠 Conceptual
expert2:00remaining
Which statement about Flask error handler decorators is true?
Select the correct statement about how Flask error handler decorators work.
Attempts:
2 left
💡 Hint
Think about what types of errors Flask errorhandler can catch.
✗ Incorrect
Flask error handlers can catch both HTTP status codes like 404 and custom exception classes.