Complete the code to import the Flask class.
from flask import [1] app = [1](__name__)
You need to import Flask to create the app instance.
Complete the code to return a JSON error response with status 404.
from flask import jsonify @app.route('/item/<int:id>') def get_item(id): item = None # pretend we look for item if not item: return jsonify({'error': 'Item not found'}), [1]
HTTP status code 404 means 'Not Found', which fits this error.
Fix the error in the code to handle 400 Bad Request errors globally.
@app.errorhandler([1]) def bad_request(error): return jsonify({'error': 'Bad request'}), 400
The error handler must match the 400 status code to catch bad requests.
Fill both blanks to create a custom error handler for 404 errors returning JSON.
@app.errorhandler([1]) def not_found(error): response = [2]({'error': 'Resource not found'}, 404) return response
The decorator uses 404 to catch not found errors, and make_response creates a response with JSON and status code.
Fill all three blanks to raise a 400 error with a custom message using abort.
from flask import abort @app.route('/submit', methods=['POST']) def submit(): data = request.json if not data or 'name' not in data: abort([1], description=[2]) return 'Success', 200 @app.errorhandler([3]) def handle_bad_request(e): return jsonify(error=str(e.description)), 400
Use abort with 400 and a description string. The error handler catches 400 errors and returns JSON with the message.
