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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #-*- coding: utf-8 -*-
  2. from werkzeug.wrappers import Response
  3. class HttpException(Exception):
  4. STATUS_STR = {
  5. 4:{
  6. 400: 'Bad request',
  7. 401: 'Unauthorized',
  8. 402: 'Payment required',
  9. 403: 'Forbidden',
  10. 404: 'Not found',
  11. 418: 'I\'m a teapot', #RFC 2324
  12. },
  13. 5:{
  14. 500: 'Internal server error',
  15. 501: 'Not implemented',
  16. },
  17. }
  18. def __init__(self, status_code = 500, tpl = 'error.html', custom = None):
  19. self.status_code = status_code
  20. self.tpl = tpl
  21. self.custom = custom
  22. def render(self, request):
  23. from .interface.template.loader import TemplateLoader
  24. loader = TemplateLoader()
  25. tpl_vars = {
  26. 'status_code': self.status_code,
  27. 'status_str': self.status_str(self.status_code),
  28. 'custom': self.custom }
  29. response = Response(
  30. loader.render_to_response(self.tpl, template_vars = tpl_vars),
  31. mimetype = 'text/html')
  32. response.status_code = self.status_code
  33. return response
  34. @staticmethod
  35. def status_str(status_code):
  36. status_fam = status_code / 100
  37. if status_fam not in HttpException.STATUS_STR or \
  38. status_code not in HttpException.STATUS_STR[status_fam]:
  39. return 'Unknown'
  40. else:
  41. return HttpException.STATUS_STR[status_fam][status_code]