view_error_management.py 1.2 KB

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