##// END OF EJS Templates
wsgi-stack: Use the pylons error handling middleware.
Martin Bornhold -
r945:c9704d4f default
parent child Browse files
Show More
@@ -31,9 +31,9 b' from pyramid.authorization import ACLAut'
31 31 from pyramid.config import Configurator
32 32 from pyramid.settings import asbool, aslist
33 33 from pyramid.wsgi import wsgiapp
34 from pyramid.httpexceptions import HTTPError, HTTPInternalServerError, HTTPFound
34 from pyramid.httpexceptions import (
35 HTTPError, HTTPInternalServerError, HTTPFound)
35 36 from pyramid.events import ApplicationCreated
36 import pyramid.httpexceptions as httpexceptions
37 37 from pyramid.renderers import render_to_response
38 38 from routes.middleware import RoutesMiddleware
39 39 import routes.util
@@ -44,10 +44,10 b' from rhodecode.config import patches'
44 44 from rhodecode.config.routing import STATIC_FILE_PREFIX
45 45 from rhodecode.config.environment import (
46 46 load_environment, load_pyramid_environment)
47 from rhodecode.lib.exceptions import VCSServerUnavailable
48 from rhodecode.lib.vcs.exceptions import VCSCommunicationError
49 47 from rhodecode.lib.middleware import csrf
50 48 from rhodecode.lib.middleware.appenlight import wrap_in_appenlight_if_enabled
49 from rhodecode.lib.middleware.error_handling import (
50 PylonsErrorHandlingMiddleware)
51 51 from rhodecode.lib.middleware.https_fixup import HttpsFixup
52 52 from rhodecode.lib.middleware.vcs import VCSMiddleware
53 53 from rhodecode.lib.plugins.utils import register_rhodecode_plugin
@@ -192,47 +192,15 b' def make_not_found_view(config):'
192 192 pylons_app = VCSMiddleware(
193 193 pylons_app, settings, appenlight_client, registry=config.registry)
194 194
195 pylons_app_as_view = wsgiapp(pylons_app)
196
197 def pylons_app_with_error_handler(context, request):
198 """
199 Handle exceptions from rc pylons app:
200
201 - old webob type exceptions get converted to pyramid exceptions
202 - pyramid exceptions are passed to the error handler view
203 """
204 def is_vcs_response(response):
205 return 'X-RhodeCode-Backend' in response.headers
206
207 def is_http_error(response):
208 # webob type error responses
209 return (400 <= response.status_int <= 599)
210
211 def is_error_handling_needed(response):
212 return is_http_error(response) and not is_vcs_response(response)
195 # Add an error handling middleware to convert errors from the old pylons
196 # app into a proper error page response.
197 reraise = (settings.get('debugtoolbar.enabled', False) or
198 rhodecode.disable_error_handler)
199 pylons_app = PylonsErrorHandlingMiddleware(
200 pylons_app, error_handler, reraise)
213 201
214 try:
215 response = pylons_app_as_view(context, request)
216 if is_error_handling_needed(response):
217 response = webob_to_pyramid_http_response(response)
218 return error_handler(response, request)
219 except HTTPError as e: # pyramid type exceptions
220 return error_handler(e, request)
221 except Exception as e:
222 log.exception(e)
223
224 if (settings.get('debugtoolbar.enabled', False) or
225 rhodecode.disable_error_handler):
226 raise
227
228 if isinstance(e, VCSCommunicationError):
229 return error_handler(VCSServerUnavailable(), request)
230
231 return error_handler(HTTPInternalServerError(), request)
232
233 return response
234
235 return pylons_app_with_error_handler
202 # Convert WSGI app to pyramid view and return it.
203 return wsgiapp(pylons_app)
236 204
237 205
238 206 def add_pylons_compat_data(registry, global_config, settings):
@@ -243,16 +211,6 b' def add_pylons_compat_data(registry, glo'
243 211 registry._pylons_compat_settings = settings
244 212
245 213
246 def webob_to_pyramid_http_response(webob_response):
247 ResponseClass = httpexceptions.status_map[webob_response.status_int]
248 pyramid_response = ResponseClass(webob_response.status)
249 pyramid_response.status = webob_response.status
250 pyramid_response.headers.update(webob_response.headers)
251 if pyramid_response.headers['content-type'] == 'text/html':
252 pyramid_response.headers['content-type'] = 'text/html; charset=UTF-8'
253 return pyramid_response
254
255
256 214 def error_handler(exception, request):
257 215 from rhodecode.model.settings import SettingsModel
258 216 from rhodecode.lib.utils2 import AttributeDict
General Comments 0
You need to be logged in to leave comments. Login now