error_handlers.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Error views for UI purposes."""
  2. from flask import Flask
  3. from werkzeug.exceptions import BadRequest, InternalServerError, HTTPException
  4. from flexmeasures.ui.utils.view_utils import render_flexmeasures_template
  5. from flexmeasures.auth import error_handling as auth_error_handling
  6. def add_html_error_views(app: Flask):
  7. """"""
  8. app.InternalServerError_handler_html = handle_500_error
  9. app.HttpException_handler_html = handle_generic_http_exception
  10. app.BadRequest_handler_html = handle_bad_request
  11. app.NotFound_handler_html = handle_not_found
  12. app.TemplateNotFound_handler_html = handle_not_found
  13. app.unauthenticated_handler_html = unauthenticated_handler
  14. app.unauthorized_handler_html = unauthorized_handler
  15. def handle_generic_http_exception(e: HTTPException):
  16. """This handles all known exception as fall-back"""
  17. error_code = 500
  18. if hasattr(e, "code") and e.code is not None:
  19. error_code = e.code
  20. error_text = getattr(e, "description", str(e))
  21. return (
  22. render_flexmeasures_template(
  23. "error.html",
  24. error_class=e.__class__.__name__,
  25. error_description="We encountered an Http exception.",
  26. error_message=error_text,
  27. ),
  28. error_code,
  29. )
  30. def handle_500_error(e: InternalServerError):
  31. return (
  32. render_flexmeasures_template(
  33. "error.html",
  34. error_class=e.__class__.__name__,
  35. error_description="We encountered an internal problem.",
  36. error_message=str(e),
  37. ),
  38. 500,
  39. )
  40. def handle_bad_request(e: BadRequest):
  41. return (
  42. render_flexmeasures_template(
  43. "error.html",
  44. error_class=e.__class__.__name__,
  45. error_description="We encountered a bad request.",
  46. error_message=e.description,
  47. ),
  48. 400,
  49. )
  50. def handle_not_found(e):
  51. return (
  52. render_flexmeasures_template(
  53. "error.html",
  54. error_class="", # standard message already includes "404: NotFound"
  55. error_description="The page you are looking for cannot be found.",
  56. error_message=str(e),
  57. ),
  58. 404,
  59. )
  60. def unauthenticated_handler():
  61. """An unauthenticated handler which renders an HTML error page"""
  62. return (
  63. render_flexmeasures_template(
  64. "error.html",
  65. error_class=auth_error_handling.UNAUTH_ERROR_CLASS,
  66. error_message=auth_error_handling.UNAUTH_MSG,
  67. ),
  68. auth_error_handling.UNAUTH_STATUS_CODE,
  69. )
  70. def unauthorized_handler():
  71. """An unauthorized handler which renders an HTML error page"""
  72. return (
  73. render_flexmeasures_template(
  74. "error.html",
  75. error_class=auth_error_handling.FORBIDDEN_ERROR_CLASS,
  76. error_message=auth_error_handling.FORBIDDEN_MSG,
  77. ),
  78. auth_error_handling.FORBIDDEN_STATUS_CODE,
  79. )