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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from werkzeug.wrappers import Response
  20. from lodel.context import LodelContext
  21. class HttpException(Exception):
  22. STATUS_STR = {
  23. 4:{
  24. 400: 'Bad request',
  25. 401: 'Unauthorized',
  26. 402: 'Payment required',
  27. 403: 'Forbidden',
  28. 404: 'Not found',
  29. 418: 'I\'m a teapot', #RFC 2324
  30. },
  31. 5:{
  32. 500: 'Internal server error',
  33. 501: 'Not implemented',
  34. },
  35. }
  36. def __init__(self, status_code = 500, tpl = 'error.html', custom = None):
  37. self.status_code = status_code
  38. self.tpl = tpl
  39. self.custom = custom
  40. ##@brief Log exception with lodel logger
  41. def log(self):
  42. LodelContext.expose_modules(globals(), {'lodel.logger': 'logger'})
  43. msg = "Webui HTTP exception : %s" % self
  44. if self.status_code / 100 == 4:
  45. logger.security(msg)
  46. elif self.status_code / 100 == 5:
  47. logger.error(msg)
  48. else:
  49. logger.warning(msg)
  50. def __str__(self):
  51. return "HTTP:%d '%s'" % (self.status_code, self.custom)
  52. def render(self, request):
  53. self.log()
  54. from .interface.template.loader import TemplateLoader
  55. loader = TemplateLoader()
  56. tpl_vars = {
  57. 'status_code': self.status_code,
  58. 'status_str': self.status_str(self.status_code),
  59. 'custom': self.custom }
  60. response = Response(
  61. loader.render_to_response(self.tpl, template_vars = tpl_vars),
  62. mimetype = 'text/html')
  63. response.status_code = self.status_code
  64. return response
  65. @staticmethod
  66. def status_str(status_code):
  67. status_fam = status_code / 100
  68. if status_fam not in HttpException.STATUS_STR or \
  69. status_code not in HttpException.STATUS_STR[status_fam]:
  70. return 'Unknown'
  71. else:
  72. return HttpException.STATUS_STR[status_fam][status_code]
  73. ##@brief Render multiple errors
  74. class HttpErrors(HttpException):
  75. def __init__(self, errors, title = None, status_code = 400):
  76. super().__init__(status_code = status_code, tpl = 'errors.html',
  77. custom = title)
  78. self.errors = errors
  79. def __str__(self):
  80. ret = super().__str__()
  81. ret += ', '.join([ '%s: %s' % val for val in self.errors.items()])
  82. return ret
  83. def render(self, request):
  84. self.log()
  85. from .interface.template.loader import TemplateLoader
  86. loader = TemplateLoader()
  87. tpl_vars = {
  88. 'status_code': self.status_code,
  89. 'errors': self.errors,
  90. 'title': self.custom }
  91. response = Response(
  92. loader.render_to_response(self.tpl, template_vars = tpl_vars),
  93. mimetype = 'text/html')
  94. response.status_code = self.status_code
  95. return response