0
0
Flaskframework~20 mins

Error handling in production in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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()
AThe user sees the '404.html' page with a 404 status code.
BThe user sees a default Flask error page with status 500.
CThe user sees the home page content 'Welcome Home!'.
DThe server crashes with a KeyError.
Attempts:
2 left
💡 Hint
Look at the @app.errorhandler decorator and what it returns.
📝 Syntax
intermediate
2: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
AChange @app.errorhandler(500) to @app.errorhandler('500').
BRemove the return statement inside the function.
CAdd a colon at the end of the function definition line.
DIndent the return statement one level less.
Attempts:
2 left
💡 Hint
Check the function definition line syntax.
🔧 Debug
advanced
2: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)
AThe error handler function is missing the error parameter.
BBecause debug=True, Flask shows the debugger instead of the custom error page.
CThe route '/' does not raise an error.
DThe 500.html template is missing.
Attempts:
2 left
💡 Hint
Think about Flask's behavior when debug mode is on.
state_output
advanced
2: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()
A200
B403
C500
D401
Attempts:
2 left
💡 Hint
Look at the return statement inside the error handler.
🧠 Conceptual
expert
2: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?
ADetailed errors can reveal sensitive information that attackers could exploit.
BDetailed errors improve user experience by showing exact problems.
CDetailed errors reduce server load by avoiding extra logging.
DDetailed errors are required to comply with HTTP standards.
Attempts:
2 left
💡 Hint
Think about security risks of exposing internal details.