- Modernized authentication with the introduction of TenantProject
- Created a base mail template - Adapt and improve document API to usage of catalogs and processors - Adapt eveai_sync to new authentication mechanism and usage of catalogs and processors
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import traceback
|
||||
|
||||
from flask import Flask, jsonify, request
|
||||
from flask_jwt_extended import get_jwt_identity, verify_jwt_in_request
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
from common.extensions import db, api_rest, jwt, minio_client, simple_encryption
|
||||
import os
|
||||
import logging.config
|
||||
@@ -45,10 +50,8 @@ def create_app(config_file=None):
|
||||
# Register Blueprints
|
||||
register_blueprints(app)
|
||||
|
||||
# Error handler for the API
|
||||
@app.errorhandler(EveAIException)
|
||||
def handle_eveai_exception(error):
|
||||
return {'message': str(error)}, error.status_code
|
||||
# Register Error Handlers
|
||||
register_error_handlers(app)
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
@@ -91,3 +94,61 @@ def register_blueprints(app):
|
||||
from .views.healthz_views import healthz_bp
|
||||
app.register_blueprint(healthz_bp)
|
||||
|
||||
|
||||
def register_error_handlers(app):
|
||||
@app.errorhandler(Exception)
|
||||
def handle_exception(e):
|
||||
"""Handle all unhandled exceptions with detailed error responses"""
|
||||
# Get the current exception info
|
||||
exc_info = traceback.format_exc()
|
||||
|
||||
# Log the full exception details
|
||||
app.logger.error(f"Unhandled exception: {str(e)}\n{exc_info}")
|
||||
|
||||
# Start with a default error response
|
||||
response = {
|
||||
"error": "Internal Server Error",
|
||||
"message": str(e),
|
||||
"type": e.__class__.__name__
|
||||
}
|
||||
|
||||
status_code = 500
|
||||
|
||||
# Handle specific types of exceptions
|
||||
if isinstance(e, HTTPException):
|
||||
status_code = e.code
|
||||
response["error"] = e.name
|
||||
|
||||
elif isinstance(e, SQLAlchemyError):
|
||||
response["error"] = "Database Error"
|
||||
response["details"] = str(e.__cause__ or e)
|
||||
|
||||
elif isinstance(e, ValueError):
|
||||
status_code = 400
|
||||
response["error"] = "Invalid Input"
|
||||
|
||||
# In development, include additional debug information
|
||||
if app.debug:
|
||||
response["debug"] = {
|
||||
"exception": exc_info,
|
||||
"class": e.__class__.__name__,
|
||||
"module": e.__class__.__module__
|
||||
}
|
||||
|
||||
return jsonify(response), status_code
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found_error(e):
|
||||
return jsonify({
|
||||
"error": "Not Found",
|
||||
"message": str(e),
|
||||
"type": "NotFoundError"
|
||||
}), 404
|
||||
|
||||
@app.errorhandler(400)
|
||||
def bad_request_error(e):
|
||||
return jsonify({
|
||||
"error": "Bad Request",
|
||||
"message": str(e),
|
||||
"type": "BadRequestError"
|
||||
}), 400
|
||||
|
||||
Reference in New Issue
Block a user