exception.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. #
  3. # cms.py - simple WSGI/Python based CMS script
  4. #
  5. # Copyright (C) 2011-2019 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #from cms.cython_support cimport * #@cy
  20. __all__ = [
  21. "CMSException",
  22. "CMSException301",
  23. "CMSPostException",
  24. ]
  25. class CMSException(Exception):
  26. __stats = {
  27. 301 : "Moved Permanently",
  28. 400 : "Bad Request",
  29. 404 : "Not Found",
  30. 405 : "Method Not Allowed",
  31. 409 : "Conflict",
  32. 500 : "Internal Server Error",
  33. }
  34. def __init__(self, httpStatusCode=500, message=""):
  35. try:
  36. httpStatus = self.__stats[httpStatusCode]
  37. except KeyError:
  38. httpStatusCode = 500
  39. httpStatus = self.__stats[httpStatusCode]
  40. self.httpStatusCode = httpStatusCode
  41. self.httpStatus = "%d %s" % (httpStatusCode, httpStatus)
  42. self.message = message
  43. def getHttpHeaders(self, resolveCallback):
  44. return ()
  45. def getHtmlHeader(self, db):
  46. return ""
  47. def getHtmlBody(self, db):
  48. return db.getString('http-error-page',
  49. '<p style="font-size: large;">%s</p>' %\
  50. self.httpStatus)
  51. class CMSException301(CMSException):
  52. # "Moved Permanently" exception
  53. def __init__(self, newUrl):
  54. CMSException.__init__(self, 301, newUrl)
  55. def url(self):
  56. return self.message
  57. def getHttpHeaders(self, resolveCallback):
  58. return ( ('Location', resolveCallback(self.url())), )
  59. def getHtmlHeader(self, db):
  60. return '<meta http-equiv="refresh" content="0; URL=%s" />' %\
  61. self.url()
  62. def getHtmlBody(self, db):
  63. return '<p style="font-size: large;">' \
  64. 'Moved permanently to ' \
  65. '<a href="%s">%s</a>' \
  66. '</p>' %\
  67. (self.url(), self.url())
  68. class CMSPostException(CMSException):
  69. def __init__(self, message=""):
  70. CMSException.__init__(self, 400, "POST handler failed: " + message)