- error handling now uses a more comprehensive error communication system.
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
from flask import Flask, jsonify, request, url_for
|
||||
from flask import Flask, jsonify, request, url_for, session as flask_session
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
import logging.config
|
||||
from jinja2 import ChoiceLoader, FileSystemLoader
|
||||
|
||||
from common.extensions import (db, bootstrap, cors, csrf, session,
|
||||
minio_client, simple_encryption, metrics, cache_manager, content_manager)
|
||||
from common.models.user import Tenant, SpecialistMagicLinkTenant
|
||||
from config.logging_config import configure_logging
|
||||
from eveai_chat_client.utils.errors import register_error_handlers
|
||||
from common.utils.errors import register_error_handlers
|
||||
from common.utils.celery_utils import make_celery, init_celery
|
||||
from common.utils.template_filters import register_filters
|
||||
from config.config import get_config
|
||||
from common.utils.chat_utils import get_default_chat_customisation
|
||||
|
||||
|
||||
def create_app(config_file=None):
|
||||
@@ -57,11 +59,23 @@ def create_app(config_file=None):
|
||||
app.celery = make_celery(app.name, app.config)
|
||||
init_celery(app.celery, app)
|
||||
|
||||
# Configure template loader with fallback to common/templates
|
||||
try:
|
||||
import os as _os
|
||||
common_templates_path = _os.path.normpath(_os.path.join(app.root_path, '..', 'common', 'templates'))
|
||||
app.jinja_loader = ChoiceLoader([
|
||||
app.jinja_loader,
|
||||
FileSystemLoader(common_templates_path),
|
||||
])
|
||||
app.logger.debug(f"Added common templates path: {common_templates_path}")
|
||||
except Exception as e:
|
||||
app.logger.error(f"Failed to configure ChoiceLoader for common templates: {e}")
|
||||
|
||||
# Register Blueprints
|
||||
register_blueprints(app)
|
||||
|
||||
# Register Error Handlers
|
||||
register_error_handlers(app)
|
||||
# Register Error Handlers (shared, profile-aware)
|
||||
register_error_handlers(app, profile='chat_client')
|
||||
|
||||
# Register Cache Handlers
|
||||
register_cache_handlers(app)
|
||||
@@ -85,6 +99,19 @@ def create_app(config_file=None):
|
||||
# Register template filters
|
||||
register_filters(app)
|
||||
|
||||
# Always inject chat customisation for templates (safe defaults)
|
||||
@app.context_processor
|
||||
def inject_customisation():
|
||||
try:
|
||||
tm = flask_session.get('tenant_make')
|
||||
options = None
|
||||
if tm and isinstance(tm, dict):
|
||||
options = tm.get('chat_customisation_options')
|
||||
customisation = get_default_chat_customisation(options)
|
||||
except Exception:
|
||||
customisation = get_default_chat_customisation(None)
|
||||
return {'customisation': customisation}
|
||||
|
||||
app.logger.info(f"EveAI Chat Client Started Successfully (PID: {os.getpid()})")
|
||||
app.logger.info("-------------------------------------------------------------------------------------------------")
|
||||
|
||||
@@ -115,8 +142,7 @@ def register_extensions(app):
|
||||
def register_blueprints(app):
|
||||
from .views.chat_views import chat_bp
|
||||
app.register_blueprint(chat_bp)
|
||||
from .views.error_views import error_bp
|
||||
app.register_blueprint(error_bp)
|
||||
# Do not register local error blueprint; use shared error handlers from common
|
||||
from .views.healthz_views import healthz_bp
|
||||
app.register_blueprint(healthz_bp)
|
||||
|
||||
|
||||
@@ -9,31 +9,31 @@ from common.utils.eveai_exceptions import EveAINoSessionTenant
|
||||
def not_found_error(error):
|
||||
current_app.logger.error(f"Not Found Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error.html', message="Page not found."), 404
|
||||
return render_template('error/404.html'), 404
|
||||
|
||||
|
||||
def internal_server_error(error):
|
||||
current_app.logger.error(f"Internal Server Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error.html', message="Internal server error."), 500
|
||||
return render_template('error/500.html'), 500
|
||||
|
||||
|
||||
def not_authorised_error(error):
|
||||
current_app.logger.error(f"Not Authorised Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error.html', message="Not authorized."), 401
|
||||
return render_template('error/401.html'), 401
|
||||
|
||||
|
||||
def access_forbidden(error):
|
||||
current_app.logger.error(f"Access Forbidden: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error.html', message="Access forbidden."), 403
|
||||
return render_template('error/403.html'), 403
|
||||
|
||||
|
||||
def key_error_handler(error):
|
||||
current_app.logger.error(f"Key Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error.html', message="An unexpected error occurred."), 500
|
||||
return render_template('error/500.html'), 500
|
||||
|
||||
|
||||
def attribute_error_handler(error):
|
||||
@@ -41,7 +41,7 @@ def attribute_error_handler(error):
|
||||
error_msg = str(error)
|
||||
current_app.logger.error(f"AttributeError: {error_msg}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error.html', message="An application error occurred."), 500
|
||||
return render_template('error/500.html'), 500
|
||||
|
||||
|
||||
def no_tenant_selected_error(error):
|
||||
|
||||
@@ -37,9 +37,7 @@ def log_after_request(response):
|
||||
|
||||
@chat_bp.route('/')
|
||||
def index():
|
||||
customisation = get_default_chat_customisation()
|
||||
return render_template('error.html', message="Please use a valid magic link to access the chat.",
|
||||
customisation=customisation)
|
||||
return render_template('error/404.html'), 404
|
||||
|
||||
|
||||
@chat_bp.route('/<magic_link_code>')
|
||||
@@ -53,14 +51,14 @@ def chat(magic_link_code):
|
||||
|
||||
if not magic_link_tenant:
|
||||
current_app.logger.error(f"Invalid magic link code: {magic_link_code}")
|
||||
return render_template('error.html', message="Invalid magic link code.")
|
||||
return render_template('error/404.html'), 404
|
||||
|
||||
# Get tenant information
|
||||
tenant_id = magic_link_tenant.tenant_id
|
||||
tenant = Tenant.query.get(tenant_id)
|
||||
if not tenant:
|
||||
current_app.logger.error(f"Tenant not found for ID: {tenant_id}")
|
||||
return render_template('error.html', message="Tenant not found.")
|
||||
return render_template('error/404.html'), 404
|
||||
# Switch to tenant schema
|
||||
Database(tenant_id).switch_schema()
|
||||
|
||||
@@ -68,19 +66,19 @@ def chat(magic_link_code):
|
||||
specialist_ml = SpecialistMagicLink.query.filter_by(magic_link_code=magic_link_code).first()
|
||||
if not specialist_ml:
|
||||
current_app.logger.error(f"Specialist magic link not found in tenant schema: {tenant_id}")
|
||||
return render_template('error.html', message="Specialist configuration not found.")
|
||||
return render_template('error/404.html'), 404
|
||||
|
||||
# Get relevant TenantMake
|
||||
tenant_make = TenantMake.query.get(specialist_ml.tenant_make_id)
|
||||
if not tenant_make:
|
||||
current_app.logger.error(f"Tenant make not found: {specialist_ml.tenant_make_id}")
|
||||
return render_template('error.html', message="Tenant make not found.")
|
||||
return render_template('error/500.html'), 500
|
||||
|
||||
# Get specialist details
|
||||
specialist = Specialist.query.get(specialist_ml.specialist_id)
|
||||
if not specialist:
|
||||
current_app.logger.error(f"Specialist not found: {specialist_ml.specialist_id}")
|
||||
return render_template('error.html', message="Specialist not found.")
|
||||
return render_template('error/404.html'), 404
|
||||
|
||||
# Store necessary information in session
|
||||
session['tenant'] = tenant.to_dict()
|
||||
@@ -124,7 +122,7 @@ def chat(magic_link_code):
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error in chat view: {str(e)}", exc_info=True)
|
||||
return render_template('error.html', message="An error occurred while setting up the chat.")
|
||||
return render_template('error/500.html'), 500
|
||||
|
||||
|
||||
@chat_bp.route('/api/send_message', methods=['POST'])
|
||||
|
||||
Reference in New Issue
Block a user