Bird
Raised Fist0
Flaskframework~20 mins

Error handler decorators in Flask - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of using @app.errorhandler decorator in Flask?
easy
A. To catch specific errors and return custom responses
B. To define routes for the application
C. To start the Flask server
D. To connect to a database

Solution

  1. Step 1: Understand the role of error handlers

    Error handlers catch errors like 404 or 500 and let you respond with custom messages.
  2. Step 2: Recognize the decorator's purpose

    @app.errorhandler is used to register functions that handle specific errors gracefully.
  3. Final Answer:

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

    Error handler = Custom error response [OK]
Hint: Error handlers catch errors and customize replies [OK]
Common Mistakes:
  • Confusing errorhandler with route decorator
  • Thinking it starts the server
  • Assuming it manages database connections
2. Which of the following is the correct syntax to handle 404 errors in Flask?
easy
A. @app.errorhandler('404') def handle_404(error): return 'Not Found', 404
B. @app.errorhandler(404) def handle_404(error): return 'Not Found', 404
C. @app.route(404) def handle_404(): return 'Not Found'
D. @app.errorhandler(HTTP_404) def handle_404(error): return 'Not Found', 404

Solution

  1. Step 1: Identify correct decorator usage

    The @app.errorhandler decorator takes an integer error code like 404, not a string or undefined constant.
  2. Step 2: Check function signature and return

    The handler function must accept an error argument and return a response with status code.
  3. Final Answer:

    @app.errorhandler(404) def handle_404(error): return 'Not Found', 404 -> Option B
  4. Quick Check:

    Use integer error code with errorhandler [OK]
Hint: Use integer error code, not string or constants [OK]
Common Mistakes:
  • Using @app.route instead of @app.errorhandler
  • Passing error code as string '404'
  • Using undefined constants like HTTP_404
3. What will be the output when a 500 error occurs with this code?
@app.errorhandler(500)
def internal_error(error):
    return 'Server Error Occurred', 500

@app.route('/')
def index():
    raise Exception('Oops')
medium
A. Server Error Occurred with status code 500
B. Exception traceback shown in browser
C. 404 Not Found error page
D. Empty response with status code 200

Solution

  1. Step 1: Understand error raising in route

    The index route raises a generic Exception, which triggers a 500 Internal Server Error.
  2. Step 2: Check error handler for 500

    The @app.errorhandler(500) catches this error and returns the custom message with status 500.
  3. Final Answer:

    Server Error Occurred with status code 500 -> Option A
  4. Quick Check:

    500 error triggers custom handler message [OK]
Hint: Exception triggers 500 error handler message [OK]
Common Mistakes:
  • Expecting default Flask error page instead of custom
  • Confusing 404 with 500 error
  • Assuming no response is returned
4. Identify the error in this Flask error handler code:
@app.errorhandler(403)
def forbidden():
    return 'Forbidden Access', 403
medium
A. Incorrect error code used in decorator
B. Return statement missing status code
C. Missing error parameter in handler function
D. Decorator should be @app.route instead

Solution

  1. Step 1: Check function signature for error handlers

    Error handler functions must accept one parameter for the error object.
  2. Step 2: Identify missing parameter

    The function forbidden has no parameters, so Flask will raise a TypeError.
  3. Final Answer:

    Missing error parameter in handler function -> Option C
  4. Quick Check:

    Error handler needs error argument [OK]
Hint: Error handler functions must accept error argument [OK]
Common Mistakes:
  • Omitting the error parameter in handler function
  • Using @app.route instead of @app.errorhandler
  • Forgetting to return status code with response
5. You want to handle both 404 and 405 errors with the same function in Flask. Which code correctly achieves this?
hard
A. @app.errorhandler(404, 405) def handle_errors(error): return f'Error {error.code} occurred', error.code
B. @app.errorhandler([404, 405]) def handle_errors(error): return f'Error {error.code} occurred', error.code
C. @app.errorhandler(404 or 405) def handle_errors(error): return f'Error {error.code} occurred', error.code
D. @app.errorhandler(404) @app.errorhandler(405) def handle_errors(error): return f'Error {error.code} occurred', error.code

Solution

  1. Step 1: Understand Flask errorhandler decorator usage

    Flask does not accept a list or multiple codes in one decorator call; you must stack decorators for multiple errors.
  2. Step 2: Identify correct stacking syntax

    Using two @app.errorhandler decorators stacked on the same function handles both errors with one function.
  3. Final Answer:

    @app.errorhandler(404) @app.errorhandler(405) def handle_errors(error): return f'Error {error.code} occurred', error.code -> Option D
  4. Quick Check:

    Stack decorators for multiple error codes [OK]
Hint: Stack multiple @app.errorhandler decorators for several errors [OK]
Common Mistakes:
  • Passing list or multiple codes in one decorator
  • Using logical OR inside decorator argument
  • Trying to pass multiple arguments to one decorator
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