view_error_management.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import View.view_privilege as privileges
  2. import logging
  3. from flask import jsonify
  4. from utility.app_logging import logger_name
  5. logger = logging.getLogger(logger_name + ".VIEW")
  6. import traceback
  7. def define_error_management(app) :
  8. @app.errorhandler(500)
  9. def handle_error(err) :
  10. """Return JSON instead of HTML for any other server error"""
  11. logger.error(f"Unknown Exception: {str(err)}")
  12. logger.debug(''.join(traceback.format_exception(etype=type(err), value=err, tb=err.__traceback__)))
  13. response = {"error": str(err) }
  14. return jsonify(response), 500
  15. @app.errorhandler(privileges.PrivilegeError)
  16. def handle_privilege_error(err) :
  17. """Return JSON instead of HTML for any other server error"""
  18. logger.error(f"PrivilegeError Exception: {str(err)}")
  19. response = {"authorization error ": str(err) }
  20. return jsonify(response), 500
  21. @app.errorhandler(Exception)
  22. def handle_privilege_error(err) :
  23. """Return JSON instead of HTML for any other server error"""
  24. logger.error(f"Exception: {str(err)}")
  25. response = {"error": str(err) }
  26. return jsonify(response), 500