[z3-checkins] r33005 - z3/jsonserver/trunk

jwashin at codespeak.net jwashin at codespeak.net
Sun Oct 8 20:18:07 CEST 2006


Author: jwashin
Date: Sun Oct  8 20:18:05 2006
New Revision: 33005

Modified:
   z3/jsonserver/trunk/JSONViews.txt
   z3/jsonserver/trunk/jsonrpc.py
Log:
a bit more error handling for JSONView


Modified: z3/jsonserver/trunk/JSONViews.txt
==============================================================================
--- z3/jsonserver/trunk/JSONViews.txt	(original)
+++ z3/jsonserver/trunk/JSONViews.txt	Sun Oct  8 20:18:05 2006
@@ -81,13 +81,14 @@
 
 Pretty cool, yes?  This is much smaller than a similar xmlrpc response.
 
-But what about parameters?  Let's create another class with some error handling.
-There's no real standard on error handling in JSON, so you may need to commune
-with the client implementation to see how to handle errors.
+But what about parameters?  Let's create another class with a parameter and an
+optional parameter.  We'll also do some local error handling.  There's no real 
+standard on error handling in JSON, so you may need to commune with the client 
+implementation to see how to handle errors.
     >>> import decimal
     >>> class FolderStupidSum(JSONView):
     ...     """return two values and their sum"""
-    ...     def doResponse(self, a=0, b=0):
+    ...     def doResponse(self, a, b=0):
     ...         try:
     ...             a = decimal.Decimal(a)
     ...             b = decimal.Decimal(b)
@@ -131,7 +132,7 @@
 
 Let's do a couple of views.  Browser is already authenticated.  Asssure the 
 parameters in the GET match the names in the doResponse method.  Default values
-are OK, and probably a good idea.
+in the method signature are OK, and probably a good idea.
     >>> browser.open('/sum?a=5')
 
 We are expecting something that looks like '{"a":5,"sum":5,"b":0}'
@@ -149,8 +150,25 @@
     >>> browser.contents.count('15') == 1
     True
 
-Let's see if the error handling works. We should get an HTTP error and something
-like '{"a":"zzz5","b":"10","error":"bad params"}
+This request does not send enough parameters.
+    >>> browser.open('/sum')
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 500: Internal Server Error
+    >>> browser.contents
+    '{"error":"doResponse() takes at least 2 arguments (1 given)"}'
+
+This request also does not send enough parameters, because the parameter 
+provided does not match the method signature.
+    >>> browser.open('/sum?d=20')
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 500: Internal Server Error
+    >>> browser.contents
+    '{"error":"doResponse() takes at least 2 arguments (1 given)"}'
+
+Let's see if the local error handling works. We should get an HTTP error and 
+something like '{"a":"zzz5","b":"10","error":"bad params"}
     >>> browser.handleErrors = True
     >>> browser.open('/sum?a=zzz5&b=10')
     Traceback (most recent call last):

Modified: z3/jsonserver/trunk/jsonrpc.py
==============================================================================
--- z3/jsonserver/trunk/jsonrpc.py	(original)
+++ z3/jsonserver/trunk/jsonrpc.py	Sun Oct  8 20:18:05 2006
@@ -360,7 +360,11 @@
         for key in request.form.keys():
             if key in params:
                 kw[str(key)] = request.form.get(key)
-        resp = premarshal(self.doResponse(*args,**kw))
+        try:
+            resp = premarshal(self.doResponse(*args,**kw))
+        except TypeError, err:
+            request.response.setStatus('500')
+            resp = {'error':'%s' % err}
 
         if not isinstance(resp,dict) and not isinstance(resp,list):
             raise ValueError("JSON responses must be dicts or lists")


More information about the z3-checkins mailing list