exception.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ]
  24. class CMSException(Exception):
  25. __stats = {
  26. 301 : "Moved Permanently",
  27. 400 : "Bad Request",
  28. 404 : "Not Found",
  29. 405 : "Method Not Allowed",
  30. 409 : "Conflict",
  31. 500 : "Internal Server Error",
  32. }
  33. def __init__(self, httpStatusCode=500, message=""):
  34. try:
  35. httpStatus = self.__stats[httpStatusCode]
  36. except KeyError:
  37. httpStatusCode = 500
  38. httpStatus = self.__stats[httpStatusCode]
  39. self.httpStatusCode = httpStatusCode
  40. self.httpStatus = "%d %s" % (httpStatusCode, httpStatus)
  41. self.message = message
  42. def getHttpHeaders(self, resolveCallback):
  43. return []
  44. def getHtmlHeader(self, db):
  45. return ""
  46. def getHtmlBody(self, db):
  47. return db.getString('http-error-page',
  48. '<p style="font-size: large;">%s</p>' %\
  49. self.httpStatus)
  50. class CMSException301(CMSException):
  51. # "Moved Permanently" exception
  52. def __init__(self, newUrl):
  53. CMSException.__init__(self, 301, newUrl)
  54. def url(self):
  55. return self.message
  56. def getHttpHeaders(self, resolveCallback):
  57. return [ f"Location: {resolveCallback(self.url())}" ]
  58. def getHtmlHeader(self, db):
  59. return '<meta http-equiv="refresh" content="0; URL=%s" />' %\
  60. self.url()
  61. def getHtmlBody(self, db):
  62. return '<p style="font-size: large;">' \
  63. 'Moved permanently to ' \
  64. '<a href="%s">%s</a>' \
  65. '</p>' %\
  66. (self.url(), self.url())