[Zope3-checkins] SVN: Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/ - added handleErrors feature

Christian Theune ct at gocept.com
Tue Sep 19 05:04:02 EDT 2006


Log message for revision 70223:
   - added handleErrors feature
  

Changed:
  U   Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/publisher/xmlrpc/README.txt
  U   Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/testing/xmlrpc.py

-=-
Modified: Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/publisher/xmlrpc/README.txt
===================================================================
--- Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/publisher/xmlrpc/README.txt	2006-09-19 08:18:07 UTC (rev 70222)
+++ Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/publisher/xmlrpc/README.txt	2006-09-19 09:04:01 UTC (rev 70223)
@@ -278,3 +278,54 @@
   >>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
   >>> proxy.epoch()
   <DateTime u'19700101T01:00:01' at ...>
+
+Handling errors with the ServerProxy
+------------------------------------
+
+Our server proxy for functional testing also supports getting the original
+errors from Zope by not handling the errors in the publisher:
+
+
+  >>> class ExceptionDemo:
+  ...     def __init__(self, context, request):
+  ...         self.context = context
+  ...         self.request = request
+  ...
+  ...     def your_exception(self):
+  ...         raise Exception("Something went wrong!")
+
+Now we'll register it as a view:
+
+  >>> from zope.configuration import xmlconfig
+  >>> ignored = xmlconfig.string("""
+  ... <configure
+  ...     xmlns="http://namespaces.zope.org/zope"
+  ...     xmlns:xmlrpc="http://namespaces.zope.org/xmlrpc"
+  ...     >
+  ...   <!-- We only need to do this include in this example,
+  ...        Normally the include has already been done for us. -->
+  ...   <include package="zope.app.publisher.xmlrpc" file="meta.zcml" />
+  ...
+  ...   <xmlrpc:view
+  ...       for="zope.app.folder.folder.IFolder"
+  ...       methods="your_exception"
+  ...       class="zope.app.publisher.xmlrpc.README.ExceptionDemo"
+  ...       permission="zope.ManageContent"
+  ...       />
+  ... </configure>
+  ... """)
+
+Now, when we call it, we get an XML-RPC fault:
+
+  >>> proxy = ServerProxy("http://mgr:mgrpw@localhost/")
+  >>> proxy.your_exception()
+  Traceback (most recent call last):
+  Fault: <Fault -1: 'Unexpected Zope exception: Exception: Something went wrong!'>
+
+We can also give the parameter `handleErrors` to have the errors not be
+handled:
+
+  >>> proxy = ServerProxy("http://mgr:mgrpw@localhost/", handleErrors=False)
+  >>> proxy.your_exception()
+  Traceback (most recent call last):
+  Exception: Something went wrong!

Modified: Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/testing/xmlrpc.py
===================================================================
--- Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/testing/xmlrpc.py	2006-09-19 08:18:07 UTC (rev 70222)
+++ Zope3/branches/ctheune-xmlrpc-testing/src/zope/app/testing/xmlrpc.py	2006-09-19 09:04:01 UTC (rev 70223)
@@ -31,6 +31,7 @@
     """
 
     verbose = False
+    handleErrors = True
 
     def request(self, host, handler, request_body, verbose=0):
         request = "POST %s HTTP/1.0\n" % (handler,)
@@ -42,7 +43,7 @@
             request += "Authorization: %s\n" % (dict(extra_headers)["Authorization"],)
 
         request += "\n" + request_body
-        response = HTTPCaller()(request)
+        response = HTTPCaller()(request, handle_errors=self.handleErrors)
 
         errcode = response.getStatus()
         errmsg = response.getStatusString()
@@ -61,9 +62,11 @@
 
 
 def ServerProxy(uri, transport=ZopeTestTransport(), encoding=None,
-                verbose=0, allow_none=0):
+                verbose=0, allow_none=0, handleErrors=True):
     """A factory that creates a server proxy using the ZopeTestTransport
     by default.
     
     """
+    if isinstance(transport, ZopeTestTransport):
+        transport.handleErrors = handleErrors
     return xmlrpclib.ServerProxy(uri, transport, encoding, verbose, allow_none)



More information about the Zope3-Checkins mailing list