Complete the code to import the Flask function used to render templates for error pages.
from flask import Flask, [1]
The render_template function is used to render custom error pages in Flask.
Complete the code to create a custom 404 error handler function.
@app.[1](404) def not_found(error): return 'Page not found', 404
The errorhandler decorator registers a function to handle specific HTTP errors like 404.
Fix the error in the code to return a custom HTML template for 500 errors.
@app.errorhandler(500) def server_error(error): return [1]('500.html'), 500
The render_template function renders an HTML file as a response.
Fill both blanks to log errors and return a JSON response in production.
import logging @app.errorhandler(500) def internal_error(error): app.logger.[1]('Server Error: %s', error) return {'error': 'Internal Server Error'}, [2]
Use app.logger.error to log errors and return HTTP status code 500 for server errors.
Fill all three blanks to create a custom error handler that logs, renders a template, and returns the correct status code.
import logging @app.errorhandler([1]) def handle_error(error): app.logger.[2]('Error occurred: %s', error) return [3]('error.html'), [1]
For a 500 error, log with error level, render the 'error.html' template, and return status 500.