Complete the code to create an error handler for 404 errors in Flask.
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler([1]) def not_found_error(error): return render_template('404.html'), 404
The @app.errorhandler(404) decorator tells Flask to use this function when a 404 Not Found error occurs.
Complete the code to handle 500 Internal Server Error using a decorator.
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler([1]) def internal_error(error): return render_template('500.html'), 500
The @app.errorhandler(500) decorator is used to catch server errors and display a custom page.
Fix the error in the code to correctly register a handler for 403 Forbidden errors.
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler([1]) def forbidden_error(error): return render_template('403.html'), 403
The error code must be an integer, not a string or a name. So 403 is correct.
Fill both blanks to create a handler for 400 Bad Request errors that returns a JSON response.
from flask import Flask, jsonify app = Flask(__name__) @app.errorhandler([1]) def bad_request(error): response = jsonify({'error': 'Bad Request'}) response.status_code = [2] return response
Both the decorator and the status code must be 400 to handle Bad Request errors properly.
Fill both blanks to create a custom error handler for 401 Unauthorized errors that returns a JSON message.
from flask import Flask, jsonify app = Flask(__name__) @app.errorhandler([1]) def unauthorized(error): response = jsonify({'message': 'Unauthorized access'}) response.status_code = [2] return response
The error handler and status code must be 401 for Unauthorized errors. The JSON message explains the error.