[test api] extract a context manager to temporarily disable app.error_handler monkey patch stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 17 Jan 2014 09:06:40 +0100
branchstable
changeset 9427 9c13ebd45cb6
parent 9426 8e87f1e18a37
child 9428 946aded614f3
[test api] extract a context manager to temporarily disable app.error_handler monkey patch
devtools/testlib.py
--- a/devtools/testlib.py	Thu Jan 02 18:30:31 2014 +0100
+++ b/devtools/testlib.py	Fri Jan 17 09:06:40 2014 +0100
@@ -1,4 +1,4 @@
-# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -85,6 +85,28 @@
     def parse_string(self, data):
         return json.loads(data)
 
+@contextmanager
+def real_error_handling(app):
+    """By default, CubicWebTC `app` attribute (ie the publisher) is monkey
+    patched so that unexpected error are raised rather than going through the
+    `error_handler` method.
+
+    By using this context manager you disable this monkey-patching temporarily.
+    Hence when publishihng a request no error will be raised, you'll get
+    req.status_out set to an HTTP error status code and the generated page will
+    usually hold a traceback as HTML.
+
+    >>> with real_error_handling(app):
+    >>>     page = app.handle_request(req)
+    """
+    # remove the monkey patched error handler
+    fake_error_handler = app.error_handler
+    del app.error_handler
+    # return the app
+    yield app
+    # restore
+    app.error_handler = fake_error_handler
+
 # email handling, to test emails sent by an application ########################
 
 MAILBOX = []
@@ -707,17 +729,14 @@
         return self.ctrl_publish(req, ctrlid, rset)
 
     def http_publish(self, url, data=None):
-        """like `url_publish`, except this returns a http response, even in case of errors"""
+        """like `url_publish`, except this returns a http response, even in case
+        of errors. You may give form parameters using the `data` argument.
+        """
         req = self.req_from_url(url)
         if data is not None:
             req.form.update(data)
-        # remove the monkey patched error handler
-        fake_error_handler = self.app.error_handler
-        del self.app.error_handler
-        try:
+        with real_error_handling(self.app):
             result = self.app_handle_request(req, req.relative_path(False))
-        finally:
-            self.app.error_handler = fake_error_handler
         return result, req
 
     @staticmethod