0
0
Flaskframework~20 mins

Custom error pages (404, 500) 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 user visits a non-existent route?

Given this Flask app with a custom 404 error handler, what will the user see if they visit /unknown?

Flask
from flask import Flask, render_template_string
app = Flask(__name__)

@app.errorhandler(404)
def page_not_found(e):
    return render_template_string('<h1>Page Not Found</h1><p>Sorry, no page here.</p>'), 404

@app.route('/')
def home():
    return 'Welcome Home!'

if __name__ == '__main__':
    app.run()
AThe browser shows 'Welcome Home!' message.
BThe browser shows a default Flask error page with a traceback.
CThe browser shows a 404 page with 'Page Not Found' and 'Sorry, no page here.' message.
DThe browser shows a 500 Internal Server Error page.
Attempts:
2 left
💡 Hint

Think about what Flask does when a route is not found and a custom 404 handler is defined.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this custom 500 error handler

Which option correctly fixes the syntax error in this Flask 500 error handler?

Flask
from flask import Flask, render_template_string
app = Flask(__name__)

@app.errorhandler(500)
def internal_error(e):
    return render_template_string('<h1>Server Error</h1><p>Something went wrong.</p>') 500

if __name__ == '__main__':
    app.run()
ARemove 500 entirely: return render_template_string(...)
BAdd a comma before 500: return render_template_string(...) , 500
CReplace 500 with status=500: return render_template_string(..., status=500)
DPut 500 inside parentheses: return (render_template_string(...)) 500
Attempts:
2 left
💡 Hint

Remember how to return a tuple with response and status code in Flask.

state_output
advanced
2:00remaining
What is the HTTP status code returned by this custom error handler?

Consider this Flask app snippet. What HTTP status code will the client receive when a 404 error occurs?

Flask
from flask import Flask, render_template_string
app = Flask(__name__)

@app.errorhandler(404)
def not_found(e):
    return render_template_string('<h1>Oops!</h1>'), 200

if __name__ == '__main__':
    app.run()
A200
B500
C404
DNone (default Flask 404 page)
Attempts:
2 left
💡 Hint

Check the status code explicitly returned by the handler.

🔧 Debug
advanced
2:00remaining
Why does this custom 404 handler not work as expected?

Review this Flask app code. Why does the custom 404 page never show?

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello!'

@app.errorhandler(404)
def page_not_found():
    return '<h1>Not Found</h1>', 404

if __name__ == '__main__':
    app.run()
AThe error handler function is missing the required error parameter.
BThe route '/hello' conflicts with the 404 handler.
CThe return statement in the error handler is missing render_template.
DFlask does not support custom 404 handlers without blueprints.
Attempts:
2 left
💡 Hint

Check the function signature of error handlers in Flask.

🧠 Conceptual
expert
3:00remaining
Which statement about Flask custom error pages is true?

Choose the correct statement about how Flask handles custom error pages for 404 and 500 errors.

AFlask automatically uses templates named 404.html and 500.html without defining error handlers.
BFlask requires restarting the server to activate custom error handlers after defining them.
CCustom error handlers can only be defined inside blueprints, not the main app.
DCustom error handlers must return a response and the correct HTTP status code to override default error pages.
Attempts:
2 left
💡 Hint

Think about what Flask expects from error handlers to replace default pages.