I am using flask and I have my packages by feature and am using blue prints and this works nicely but I would like to have a global 404 and error page which sits outside of any specific feature package.
When I trigger a 404 flask handles this with the default 404 handler still and I dont get my custom template. Below is my code:
init.py
# Define the WSGI application object
app = Flask(__name__)
# Load Configuration
app.config.from_object('config')
from .user.route import mod_user as user_module
from .route.error_handler import mod_error as error_module
# Register blueprints
app.register_blueprint(user_module)
app.register_blueprint(error_module)
error_handler.py
import traceback
from flask import Flask, render_template, Blueprint
mod_error = Blueprint('error', __name__, template_folder='templates')
@mod_error.errorhandler(404)
def not_found(e):
print(e)
return render_template('404.html')
@mod_error.errorhandler(Exception)
def general_error(e):
print(e)
print(traceback.format_exc())
return render_template('error.html')
My feature routes are defined in project.user.route.py
Global route\error handler is in project.route.error_handler.py
Global error templates are in project.templates