No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exceptions.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #-*- coding: utf-8 -*-
  2. from werkzeug.wrappers import Response
  3. from lodel.context import LodelContext
  4. class HttpException(Exception):
  5. STATUS_STR = {
  6. 4:{
  7. 400: 'Bad request',
  8. 401: 'Unauthorized',
  9. 402: 'Payment required',
  10. 403: 'Forbidden',
  11. 404: 'Not found',
  12. 418: 'I\'m a teapot', #RFC 2324
  13. },
  14. 5:{
  15. 500: 'Internal server error',
  16. 501: 'Not implemented',
  17. },
  18. }
  19. def __init__(self, status_code = 500, tpl = 'error.html', custom = None):
  20. self.status_code = status_code
  21. self.tpl = tpl
  22. self.custom = custom
  23. ##@brief Log exception with lodel logger
  24. def log(self):
  25. LodelContext.expose_modules(globals(), {'lodel.logger': 'logger'})
  26. msg = "Webui HTTP exception : %s" % self
  27. if self.status_code / 100 == 4:
  28. logger.security(msg)
  29. elif self.status_code / 100 == 5:
  30. logger.error(msg)
  31. else:
  32. logger.warning(msg)
  33. def __str__(self):
  34. return "HTTP:%d '%s'" % (self.status_code, self.custom)
  35. def render(self, request):
  36. self.log()
  37. from .interface.template.loader import TemplateLoader
  38. loader = TemplateLoader()
  39. tpl_vars = {
  40. 'status_code': self.status_code,
  41. 'status_str': self.status_str(self.status_code),
  42. 'custom': self.custom }
  43. response = Response(
  44. loader.render_to_response(self.tpl, template_vars = tpl_vars),
  45. mimetype = 'text/html')
  46. response.status_code = self.status_code
  47. return response
  48. @staticmethod
  49. def status_str(status_code):
  50. status_fam = status_code / 100
  51. if status_fam not in HttpException.STATUS_STR or \
  52. status_code not in HttpException.STATUS_STR[status_fam]:
  53. return 'Unknown'
  54. else:
  55. return HttpException.STATUS_STR[status_fam][status_code]
  56. ##@brief Render multiple errors
  57. class HttpErrors(HttpException):
  58. def __init__(self, errors, title = None, status_code = 400):
  59. super().__init__(status_code = status_code, tpl = 'errors.html',
  60. custom = title)
  61. self.errors = errors
  62. def __str__(self):
  63. ret = super().__str__()
  64. ret += ', '.join([ '%s: %s' % val for val in self.errors.items()])
  65. return ret
  66. def render(self, request):
  67. self.log()
  68. from .interface.template.loader import TemplateLoader
  69. loader = TemplateLoader()
  70. tpl_vars = {
  71. 'status_code': self.status_code,
  72. 'errors': self.errors,
  73. 'title': self.custom }
  74. response = Response(
  75. loader.render_to_response(self.tpl, template_vars = tpl_vars),
  76. mimetype = 'text/html')
  77. response.status_code = self.status_code
  78. return response