PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/r2/r2/controllers/error.py

https://github.com/dcarvill/reddit
Python | 215 lines | 180 code | 4 blank | 31 comment | 4 complexity | 4c6725581f412d1a15604a54f325f6a0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0
  1. # The contents of this file are subject to the Common Public Attribution
  2. # License Version 1.0. (the "License"); you may not use this file except in
  3. # compliance with the License. You may obtain a copy of the License at
  4. # http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
  5. # License Version 1.1, but Sections 14 and 15 have been added to cover use of
  6. # software over a computer network and provide for limited attribution for the
  7. # Original Developer. In addition, Exhibit A has been modified to be consistent
  8. # with Exhibit B.
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  12. # the specific language governing rights and limitations under the License.
  13. #
  14. # The Original Code is Reddit.
  15. #
  16. # The Original Developer is the Initial Developer. The Initial Developer of the
  17. # Original Code is CondeNet, Inc.
  18. #
  19. # All portions of the code written by CondeNet are Copyright (c) 2006-2010
  20. # CondeNet, Inc. All Rights Reserved.
  21. ################################################################################
  22. import os.path
  23. import pylons
  24. import paste.fileapp
  25. from paste.httpexceptions import HTTPFound
  26. from pylons.middleware import error_document_template, media_path
  27. from pylons import c, request, g
  28. from pylons.i18n import _
  29. import random as rand
  30. from r2.lib.filters import safemarkdown, unsafe
  31. try:
  32. # place all r2 specific imports in here. If there is a code error, it'll get caught and
  33. # the stack trace won't be presented to the user in production
  34. from reddit_base import RedditController, Cookies
  35. from r2.models.subreddit import DefaultSR, Subreddit
  36. from r2.models.link import Link
  37. from r2.lib import pages
  38. from r2.lib.strings import strings, rand_strings
  39. from r2.lib.template_helpers import static
  40. except Exception, e:
  41. if g.debug:
  42. # if debug mode, let the error filter up to pylons to be handled
  43. raise e
  44. else:
  45. # production environment: protect the code integrity!
  46. print "HuffmanEncodingError: make sure your python compiles before deploying, stupid!"
  47. # kill this app
  48. import os
  49. os._exit(1)
  50. NUM_FAILIENS = 3
  51. redditbroke = \
  52. '''<html>
  53. <head>
  54. <title>reddit broke!</title>
  55. </head>
  56. <body>
  57. <div style="margin: auto; text-align: center">
  58. <p>
  59. <a href="/">
  60. <img border="0" src="%s" alt="you broke reddit" />
  61. </a>
  62. </p>
  63. <p>
  64. %s
  65. </p>
  66. </body>
  67. </html>
  68. '''
  69. class ErrorController(RedditController):
  70. """Generates error documents as and when they are required.
  71. The ErrorDocuments middleware forwards to ErrorController when error
  72. related status codes are returned from the application.
  73. This behaviour can be altered by changing the parameters to the
  74. ErrorDocuments middleware in your config/middleware.py file.
  75. """
  76. allowed_render_styles = ('html', 'xml', 'js', 'embed', '', "compact", 'api')
  77. # List of admins to blame (skip the first admin, "reddit")
  78. # If list is empty, just blame "an admin"
  79. admins = g.admins[1:] or ["an admin"]
  80. def __before__(self):
  81. try:
  82. c.error_page = True
  83. RedditController.__before__(self)
  84. except HTTPFound:
  85. # ignore an attempt to redirect from an error page
  86. pass
  87. except:
  88. handle_awful_failure("Error occurred in ErrorController.__before__")
  89. def __after__(self):
  90. try:
  91. RedditController.__after__(self)
  92. except:
  93. handle_awful_failure("Error occurred in ErrorController.__after__")
  94. def __call__(self, environ, start_response):
  95. try:
  96. return RedditController.__call__(self, environ, start_response)
  97. except:
  98. return handle_awful_failure("something really awful just happened.")
  99. def send403(self):
  100. c.response.status_code = 403
  101. c.site = DefaultSR()
  102. if 'usable_error_content' in request.environ:
  103. return request.environ['usable_error_content']
  104. else:
  105. res = pages.RedditError(
  106. title=_("forbidden (%(domain)s)") % dict(domain=g.domain),
  107. message=_("you are not allowed to do that"))
  108. return res.render()
  109. def send404(self):
  110. c.response.status_code = 404
  111. if 'usable_error_content' in request.environ:
  112. return request.environ['usable_error_content']
  113. return pages.RedditError(_("page not found"),
  114. _("the page you requested does not exist")).render()
  115. def send429(self):
  116. c.response.status_code = 429
  117. if 'retry_after' in request.environ:
  118. c.response.headers['Retry-After'] = str(request.environ['retry_after'])
  119. template_name = '/ratelimit_toofast.html'
  120. else:
  121. template_name = '/ratelimit_throttled.html'
  122. loader = pylons.buffet.engines['mako']['engine']
  123. template = loader.load_template(template_name)
  124. return template.render(logo_url=static(g.default_header_url))
  125. def send503(self):
  126. c.response.status_code = 503
  127. c.response.headers['Retry-After'] = request.environ['retry_after']
  128. return request.environ['usable_error_content']
  129. def GET_document(self):
  130. try:
  131. # clear cookies the old fashioned way
  132. c.cookies = Cookies()
  133. code = request.GET.get('code', '')
  134. try:
  135. code = int(code)
  136. except ValueError:
  137. code = 404
  138. srname = request.GET.get('srname', '')
  139. takedown = request.GET.get('takedown', "")
  140. if srname:
  141. c.site = Subreddit._by_name(srname)
  142. if c.render_style not in self.allowed_render_styles:
  143. if code not in (204, 304):
  144. c.response.content = str(code)
  145. return c.response
  146. elif c.render_style == "api":
  147. c.response.content = "{\"error\": %s}" % code
  148. return c.response
  149. elif takedown and code == 404:
  150. link = Link._by_fullname(takedown)
  151. return pages.TakedownPage(link).render()
  152. elif code == 403:
  153. return self.send403()
  154. elif code == 429:
  155. return self.send429()
  156. elif code == 500:
  157. randmin = {'admin': rand.choice(self.admins)}
  158. failien_name = 'youbrokeit%d.png' % rand.randint(1, NUM_FAILIENS)
  159. failien_url = static(failien_name)
  160. return redditbroke % (failien_url, rand_strings.sadmessages % randmin)
  161. elif code == 503:
  162. return self.send503()
  163. elif code == 304:
  164. if request.GET.has_key('x-sup-id'):
  165. x_sup_id = request.GET.get('x-sup-id')
  166. if '\r\n' not in x_sup_id:
  167. c.response.headers['x-sup-id'] = x_sup_id
  168. return c.response
  169. elif c.site:
  170. return self.send404()
  171. else:
  172. return "page not found"
  173. except:
  174. return handle_awful_failure("something really bad just happened.")
  175. POST_document = GET_document
  176. def handle_awful_failure(fail_text):
  177. """
  178. Makes sure that no errors generated in the error handler percolate
  179. up to the user unless debug is enabled.
  180. """
  181. if g.debug:
  182. import sys
  183. s = sys.exc_info()
  184. # reraise the original error with the original stack trace
  185. raise s[1], None, s[2]
  186. try:
  187. # log the traceback, and flag the "path" as the error location
  188. import traceback
  189. g.log.error("FULLPATH: %s" % fail_text)
  190. g.log.error(traceback.format_exc())
  191. return redditbroke % (rand.randint(1,NUM_FAILIENS), fail_text)
  192. except:
  193. # we are doomed. Admit defeat
  194. return "This is an error that should never occur. You win."