0
0
Flaskframework~20 mins

API error handling in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the HTTP status code returned by this Flask error handler?
Consider this Flask code snippet that handles a 404 error. What HTTP status code will the client receive when this error handler is triggered?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.errorhandler(404)
def not_found(error):
    return jsonify({'error': 'Not found'}), 404
A400
B500
C200
D404
Attempts:
2 left
💡 Hint
Look at the second value returned in the tuple from the error handler.
📝 Syntax
intermediate
2:00remaining
Which option correctly raises a custom error with a JSON response in Flask?
You want to raise a 400 Bad Request error with a JSON message in Flask. Which code snippet correctly does this?
A
from flask import jsonify
return jsonify({'error': 'Bad request'}), 400
B
from flask import abort
abort(400, description={'error': 'Bad request'})
C
from flask import abort
abort(jsonify({'error': 'Bad request'}), 400)
Draise Exception(jsonify({'error': 'Bad request'}), 400)
Attempts:
2 left
💡 Hint
Flask's abort function does not accept a JSON object as description.
🔧 Debug
advanced
2:00remaining
Why does this Flask error handler not return JSON as expected?
This Flask error handler is supposed to return JSON for 500 errors, but the client receives HTML instead. Why?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.errorhandler(500)
def internal_error(error):
    return {'error': 'Internal Server Error'}, 500
AThe handler returns a dict, but Flask needs jsonify() to convert it to JSON response.
BThe error handler must return a string, not a dict.
CThe status code 500 is not allowed in error handlers.
DFlask automatically converts dict to JSON, so the problem is elsewhere.
Attempts:
2 left
💡 Hint
Check how Flask converts return values to responses.
state_output
advanced
2:00remaining
What is the output when a 403 error occurs with this Flask setup?
Given this Flask app, what JSON response does the client receive when a 403 Forbidden error is triggered?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.errorhandler(403)
def forbidden(error):
    response = jsonify({'message': 'Access denied'})
    response.status_code = 403
    return response
A{'error': 'Forbidden'} with HTTP status 403
B{'message': 'Access denied'} with HTTP status 403
C{'message': 'Access denied'} with HTTP status 200
DHTML error page with HTTP status 403
Attempts:
2 left
💡 Hint
Look at the JSON content and status code set in the response object.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask error handling is true?
Choose the correct statement about how Flask handles errors and error handlers.
AFlask automatically converts any returned dict from error handlers into JSON responses.
BFlask error handlers only work for HTTP errors raised by abort(), not for exceptions.
CFlask error handlers can catch exceptions globally and return custom responses for any HTTP status code.
DFlask requires error handlers to return plain strings, not JSON or response objects.
Attempts:
2 left
💡 Hint
Think about how Flask errorhandler decorator works with exceptions and HTTP codes.