import View.view_privilege as privileges
import logging
from flask import jsonify
from utility.app_logging import logger_name
logger = logging.getLogger(logger_name + ".VIEW")

import traceback
def define_error_management(app) :
    @app.errorhandler(500)
    def handle_error(err) :
        """Return JSON instead of HTML for any other server error"""
        logger.error(f"Unknown Exception: {str(err)}")
        logger.debug(''.join(traceback.format_exception(etype=type(err), value=err, tb=err.__traceback__)))
        response = {"error": str(err) }
        return jsonify(response), 500

    @app.errorhandler(privileges.APIAuthError)
    def handle_error(err) :
        """Return JSON instead of HTML for any other server error"""
        logger.error(f"Unknown Exception: {str(err)}")
        logger.debug(''.join(traceback.format_exception(etype=type(err), value=err, tb=err.__traceback__)))
        response = {"error": str(err) }
        return jsonify(response), 500
