Challenge - 5 Problems
API Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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
Attempts:
2 left
💡 Hint
Look at the second value returned in the tuple from the error handler.
✗ Incorrect
The error handler returns a tuple with a JSON response and the status code 404, so the client receives a 404 status.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Flask's abort function does not accept a JSON object as description.
✗ Incorrect
Option A returns a JSON response with status 400 correctly. Option A misuses abort with a dict description, C misuses abort with jsonify, and D raises a generic Exception which is not handled.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check how Flask converts return values to responses.
✗ Incorrect
Returning a dict alone does not produce a JSON response. Flask requires jsonify() to create a proper JSON response with correct headers.
❓ state_output
advanced2: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
Attempts:
2 left
💡 Hint
Look at the JSON content and status code set in the response object.
✗ Incorrect
The handler returns a JSON with key 'message' and sets status code 403 explicitly, so client gets that JSON and status 403.
🧠 Conceptual
expert2:00remaining
Which statement about Flask error handling is true?
Choose the correct statement about how Flask handles errors and error handlers.
Attempts:
2 left
💡 Hint
Think about how Flask errorhandler decorator works with exceptions and HTTP codes.
✗ Incorrect
Flask error handlers can catch exceptions or HTTP errors globally and return any custom response including JSON with any status code.