[bwcompat] use cubicweb error views (closes #4545130)
authorJulien Cristau <julien.cristau@logilab.fr>
Tue, 30 Jun 2015 11:15:54 +0200
changeset 11578 fcba04437236
parent 11577 694d043dabc1
child 11579 78ba3e88a549
[bwcompat] use cubicweb error views (closes #4545130)
pyramid_cubicweb/bwcompat.py
--- a/pyramid_cubicweb/bwcompat.py	Thu Jun 18 10:49:34 2015 +0200
+++ b/pyramid_cubicweb/bwcompat.py	Tue Jun 30 11:15:54 2015 +0200
@@ -1,3 +1,5 @@
+import sys
+
 from pyramid import security
 from pyramid import tweens
 from pyramid.httpexceptions import HTTPSeeOther
@@ -8,7 +10,7 @@
 
 from cubicweb.web.application import CubicWebPublisher
 
-from cubicweb.web import LogOut
+from cubicweb.web import LogOut, PublishException
 
 from pyramid_cubicweb.core import cw_to_pyramid
 
@@ -92,9 +94,6 @@
                 # for this exception) should be enough
                 # content = self.appli.ajax_error_handler(req, ex)
                 raise
-            except cubicweb.web.NotFound as ex:
-                raise httpexceptions.HTTPNotFound(ex.message)
-
             if content is not None:
                 request.response.body = content
 
@@ -119,6 +118,31 @@
 
         return request.response
 
+    def error_handler(self, exc, request):
+        req = request.cw_request
+        if isinstance(exc, httpexceptions.HTTPException):
+            request.response = exc
+        elif isinstance(exc, PublishException) and exc.status is not None:
+            request.response = httpexceptions.exception_response(exc.status)
+        else:
+            request.response = httpexceptions.HTTPInternalServerError()
+        request.response.cache_control = 'no-cache'
+        vreg = request.registry['cubicweb.registry']
+        excinfo = sys.exc_info()
+        req.reset_message()
+        if req.ajax_request:
+            content = self.appli.ajax_error_handler(req, exc)
+        else:
+            try:
+                req.data['ex'] = exc
+                errview = vreg['views'].select('error', req)
+                template = self.appli.main_template_id(req)
+                content = vreg['views'].main_template(req, template, view=errview)
+            except Exception:
+                content = vreg['views'].main_template(req, 'error-template')
+        request.response.body = content
+        return request.response
+
 
 class TweenHandler(object):
     """ A Pyramid tween handler that submit unhandled requests to a Cubicweb
@@ -172,3 +196,6 @@
 
     config.add_tween(
         'pyramid_cubicweb.bwcompat.TweenHandler', under=tweens.EXCVIEW)
+    config.add_view(cwhandler.error_handler, context=Exception)
+    # XXX why do i need this?
+    config.add_view(cwhandler.error_handler, context=httpexceptions.HTTPForbidden)