error.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from pathlib import Path
  2. from typing import Union
  3. from .elements.column import Column as column
  4. from .elements.html import Html as html
  5. from .elements.label import Label as label
  6. SAD_FACE_SVG = (Path(__file__).parent / 'static' / 'sad_face.svg').read_text(encoding='utf-8')
  7. def error_content(status_code: int, exception: Union[str, Exception] = '') -> None:
  8. """Create an error page.
  9. :param status_code: HTTP status code
  10. :param exception: exception that caused the error
  11. """
  12. if 400 <= status_code <= 499:
  13. title = "This page doesn't exist."
  14. elif 500 <= status_code <= 599:
  15. title = 'Server error'
  16. else:
  17. title = 'Unknown error'
  18. if isinstance(exception, str):
  19. message = exception
  20. else:
  21. message = exception.__class__.__name__
  22. if str(exception):
  23. message += ': ' + str(exception)
  24. with column().style('width: 100%; padding: 5rem 0; align-items: center; gap: 0'):
  25. html(SAD_FACE_SVG).style('width: 8rem; padding: 1.25rem 0')
  26. label(str(status_code)).style('font-size: 3.75rem; line-height: 1; padding: 1.25rem 0')
  27. label(title).style('font-size: 1.25rem; line-height: 1.75rem; padding: 1.25rem 0')
  28. label(message).style('font-size: 1.125rem; line-height: 1.75rem; color: rgb(107 114 128)')