exception.py 913 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. # encoding:utf-8
  3. from . import app
  4. from .constant import error_response
  5. class BizException(Exception):
  6. def __init__(self, message, status_code=400):
  7. super().__init__(message)
  8. self.message = message
  9. self.status_code = status_code
  10. @app.errorhandler(BizException)
  11. def handle_biz_exception(error):
  12. return error_response(msg=str(error), http_code=200)
  13. # Generic error handler for uncaught exceptions
  14. @app.errorhandler(Exception)
  15. def handle_generic_exception(error):
  16. return error_response(msg=str(error), http_code=500)
  17. # Specific error handler for 404 Not Found errors
  18. @app.errorhandler(404)
  19. def handle_404_error(error):
  20. return error_response(msg="Resource not found", http_code=404)
  21. # Specific error handler for 400 Bad Request errors
  22. @app.errorhandler(400)
  23. def handle_400_error(error):
  24. return error_response(msg="Bad Request", http_code=400)