Bird
Raised Fist0
Flaskframework~20 mins

Error handling in production 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 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.

Practice

(1/5)
1. What is the main purpose of using @app.errorhandler in a Flask application?
easy
A. To catch specific errors and provide custom responses
B. To speed up the application startup
C. To automatically restart the server on errors
D. To log user activities

Solution

  1. Step 1: Understand the role of @app.errorhandler

    This decorator is used to catch specific HTTP or application errors in Flask.
  2. Step 2: Recognize its use for custom responses

    It allows developers to return user-friendly error pages or messages instead of default error traces.
  3. Final Answer:

    To catch specific errors and provide custom responses -> Option A
  4. Quick Check:

    Error handling = custom responses [OK]
Hint: Errorhandler decorates functions to catch and respond to errors [OK]
Common Mistakes:
  • Thinking it restarts the server automatically
  • Confusing errorhandler with logging functions
  • Assuming it speeds up app startup
2. Which of the following is the correct syntax to handle 404 errors in Flask?
easy
A. @app.error(404) def not_found(error): return 'Not found', 404
B. @app.errorhandler(404) def not_found(error): return 'Page not found', 404
C. @app.handle_error(404) def not_found(): return 'Page missing', 404
D. @app.catch_error(404) def not_found(error): return '404 error', 404

Solution

  1. Step 1: Identify the correct decorator name

    The correct Flask decorator for error handling is @app.errorhandler.
  2. Step 2: Confirm function signature and return

    The function should accept an error parameter and return a response with the error code.
  3. Final Answer:

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

    Correct decorator and function signature = @app.errorhandler(404) def not_found(error): return 'Page not found', 404 [OK]
Hint: Use @app.errorhandler with error code and error param [OK]
Common Mistakes:
  • Using wrong decorator names like @app.handle_error
  • Omitting the error parameter in the function
  • Returning without the status code
3. Given this Flask code snippet, what will the user see if a 500 error occurs?
@app.errorhandler(500)
def internal_error(error):
    return 'Something went wrong, please try later.', 500
medium
A. The message: 'Something went wrong, please try later.' with status 500
B. A detailed Python traceback error page
C. A blank page with status code 500
D. The default Flask 404 error page

Solution

  1. Step 1: Understand the errorhandler function

    The function returns a custom message and status code 500 when a 500 error occurs.
  2. Step 2: Identify the user-visible output

    The user will see the returned string message with HTTP status 500, not the default traceback.
  3. Final Answer:

    The message: 'Something went wrong, please try later.' with status 500 -> Option A
  4. Quick Check:

    Custom error message shown = The message: 'Something went wrong, please try later.' with status 500 [OK]
Hint: Custom errorhandler returns your message, not default trace [OK]
Common Mistakes:
  • Expecting Flask's default error page instead of custom message
  • Thinking a blank page will appear
  • Confusing 500 error with 404 error page
4. What is wrong with this Flask error handler code?
@app.errorhandler(403)
def forbidden():
    return 'Access denied', 403
medium
A. The return statement must include a JSON response
B. The decorator should be @app.handle_error
C. The function must accept an error parameter
D. The status code 403 is invalid

Solution

  1. Step 1: Check function signature for errorhandler

    Flask error handler functions must accept the error argument to receive error details.
  2. Step 2: Identify missing error parameter

    The function forbidden lacks the required error parameter.
  3. Final Answer:

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

    Error handler function needs error param = The function must accept an error parameter [OK]
Hint: Error handler functions always take an error argument [OK]
Common Mistakes:
  • Omitting the error parameter in handler functions
  • Using wrong decorator names
  • Assuming JSON response is mandatory
5. You want to show a friendly error page for all unhandled exceptions in production without exposing debug info. Which Flask setup is best?
hard
A. Use @app.route('/error') to manually show errors
B. Set app.debug = True to see detailed errors
C. Remove all error handlers to let Flask show default error pages
D. Use @app.errorhandler(Exception) to catch all errors and return a simple message

Solution

  1. Step 1: Understand catching all exceptions

    Using @app.errorhandler(Exception) catches all unhandled errors globally.
  2. Step 2: Provide user-friendly message without debug info

    Returning a simple message hides technical details, improving user experience and security.
  3. Final Answer:

    Use @app.errorhandler(Exception) to catch all errors and return a simple message -> Option D
  4. Quick Check:

    Global error handler with simple message = Use @app.errorhandler(Exception) to catch all errors and return a simple message [OK]
Hint: Catch Exception globally to hide debug info in production [OK]
Common Mistakes:
  • Enabling debug mode in production
  • Relying on default Flask error pages
  • Using routes instead of error handlers for errors
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