Bird
Raised Fist0
Flaskframework~20 mins

Custom error pages (404, 500) in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating custom error pages like 404 or 500 in a Flask app?
easy
A. To show friendly messages instead of default error text
B. To speed up the server response time
C. To disable error logging
D. To automatically fix bugs in the code

Solution

  1. Step 1: Understand default error behavior

    By default, Flask shows plain error messages like '404 Not Found' which are not user-friendly.
  2. Step 2: Purpose of custom error pages

    Custom error pages replace these default messages with nicer, clearer pages that help users understand the problem.
  3. Final Answer:

    To show friendly messages instead of default error text -> Option A
  4. Quick Check:

    Custom error pages = Friendly messages [OK]
Hint: Custom error pages improve user experience with friendly messages [OK]
Common Mistakes:
  • Thinking custom pages fix server speed
  • Believing they disable error logs
  • Assuming they auto-correct bugs
2. Which of the following is the correct way to define a custom 404 error handler in Flask?
easy
A. @app.handle_error(404) def not_found(error): return 'Page not found', 404
B. @app.route('/404') def not_found(): return 'Page not found', 404
C. @app.errorhandler(404) def not_found(error): return 'Page not found', 404
D. @app.error(404) def not_found(error): return 'Page not found', 404

Solution

  1. Step 1: Identify correct decorator for error handling

    Flask uses @app.errorhandler(error_code) to register error handlers.
  2. Step 2: Check function signature and return

    The handler function takes an error argument and returns a response with the error code.
  3. Final Answer:

    @app.errorhandler(404)\ndef not_found(error):\n return 'Page not found', 404 -> Option C
  4. Quick Check:

    @app.errorhandler(404) = Correct syntax [OK]
Hint: Use @app.errorhandler(code) to define error handlers [OK]
Common Mistakes:
  • Using @app.route instead of @app.errorhandler
  • Wrong decorator names like @app.handle_error
  • Missing error parameter in handler function
3. Given this Flask code snippet, what will be the output when a user visits a non-existing page?
@app.errorhandler(404)
def page_not_found(e):
    return '

Oops! Page missing.

', 404 @app.route('/') def home(): return 'Welcome Home'
medium
A. The browser shows 'Welcome Home'
B. The server crashes with an error
C. The browser shows default Flask 404 page
D. The browser shows '

Oops! Page missing.

' with 404 status

Solution

  1. Step 1: Understand route and error handler

    The '/' route returns 'Welcome Home'. The 404 error handler returns a custom HTML message with status 404.
  2. Step 2: Accessing a non-existing page triggers 404

    Visiting a page not defined triggers the 404 handler, so the custom message is returned with status 404.
  3. Final Answer:

    The browser shows '<h1>Oops! Page missing.</h1>' with 404 status -> Option D
  4. Quick Check:

    404 error triggers custom handler = The browser shows '<h1>Oops! Page missing.</h1>' with 404 status [OK]
Hint: Non-existing pages trigger 404 handler, not normal routes [OK]
Common Mistakes:
  • Thinking '/' route handles all pages
  • Expecting default Flask 404 page instead of custom
  • Assuming server crashes on 404
4. Identify the error in this Flask code for a custom 500 error page:
@app.errorhandler(500)
def internal_error():
    return 'Server error occurred', 500
medium
A. The handler function must accept an error parameter
B. The decorator should be @app.handle_error(500)
C. The return statement must not include the status code
D. The function name must be 'handle_500_error'

Solution

  1. Step 1: Check function signature for error handlers

    Flask error handlers must accept one argument for the error object.
  2. Step 2: Verify the given function signature

    The function internal_error() has no parameters, which is incorrect for error handlers.
  3. Final Answer:

    The handler function must accept an error parameter -> Option A
  4. Quick Check:

    Error handler functions need error argument [OK]
Hint: Error handlers always take one error argument [OK]
Common Mistakes:
  • Omitting the error parameter in handler function
  • Using wrong decorator names
  • Thinking function name matters for error handlers
5. You want to create a custom 404 error page that renders an HTML template named '404.html' and returns status 404. Which Flask code snippet correctly does this?
hard
A. @app.route('/404') def not_found(): return render_template('404.html'), 404
B. @app.errorhandler(404) def not_found(error): return render_template('404.html'), 404
C. @app.errorhandler(404) def not_found(): return render_template('404.html')
D. @app.errorhandler(404) def not_found(error): return render_template('404.html', status=404)

Solution

  1. Step 1: Use correct decorator and function signature

    Use @app.errorhandler(404) and define function with error parameter.
  2. Step 2: Return rendered template with status code

    Return render_template('404.html') and the status code 404 as a tuple.
  3. Final Answer:

    @app.errorhandler(404)\ndef not_found(error):\n return render_template('404.html'), 404 -> Option B
  4. Quick Check:

    Custom 404 with template and status code = @app.errorhandler(404) def not_found(error): return render_template('404.html'), 404 [OK]
Hint: Return (render_template, status_code) tuple in error handler [OK]
Common Mistakes:
  • Using @app.route instead of @app.errorhandler
  • Missing error parameter in function
  • Not returning status code with template
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard