0
0
Flaskframework~20 mins

Error handler decorators in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Error Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
AA plain text response 'Not found' with status code 200
BA JSON response with {'error': 'Not found'} and status code 404
CA redirect to '/' route
DA server error 500 response
Attempts:
2 left
💡 Hint
Look at the @app.errorhandler decorator and what it returns.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a 500 error handler?
Choose the correct Flask error handler syntax for HTTP 500 errors.
A
@app.errorhandler(500)
def server_error():
    return 'Server error', 500
B
@app.errorhandler(500):
def server_error(e):
    return 'Server error', 500
C
@app.errorhandler(500)
def server_error(e):
    return 'Server error', 500
D
@app.errorhandler(500)
server_error = lambda e: ('Server error', 500)
Attempts:
2 left
💡 Hint
Check the decorator syntax and function parameters.
🔧 Debug
advanced
2: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)
AThe route '/secret' must be decorated with @app.errorhandler(403) instead.
BThe abort(403) call is missing import and causes a NameError.
CThe errorhandler decorator should be @app.errorhandler('403') with quotes.
DThe handler function must accept an error parameter but 'forbidden' does not.
Attempts:
2 left
💡 Hint
Check the function signature of the error handler.
state_output
advanced
2: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()
A418
B500
C200
D404
Attempts:
2 left
💡 Hint
Look at the status code returned by the error handler.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask error handler decorators is true?
Select the correct statement about how Flask error handler decorators work.
AError handlers can catch both HTTP status codes and custom exception classes.
BError handlers must always return plain text responses, JSON is not supported.
CMultiple error handlers for the same error code can be defined and all will run.
DError handlers only work for errors raised inside route functions, not middleware.
Attempts:
2 left
💡 Hint
Think about what types of errors Flask errorhandler can catch.