Permalink
Browse files

Added support for Decimal (emitted as float) and better error handlin…

…g when converting to json
  • Loading branch information...
deavid committed Apr 22, 2011
1 parent b4186a0 commit 575b08bb90b465c42c7e9c621c27493c2e94b5fe
Showing with 16 additions and 1 deletion.
  1. +16 −1 bjsonrpc/connection.py
View
@@ -312,6 +312,9 @@ def dump_object(self, obj):
if not hasattr(obj, '__class__'):
raise TypeError("JSON objects must be instances, not types")
+ if obj.__class__.__name__ == 'Decimal': # Probably is just a float.
+ return float(obj)
+
if isinstance(obj, RemoteObject):
return self._dump_objectreference(obj)
@@ -548,8 +551,20 @@ def dispatch_item_single(self, item):
}
if response is not None:
+ txtResponse = None
+ try:
+ txtResponse = json.dumps(response, self)
+ except Exception, e:
+ print "An unexpected error ocurred when trying to create the message:", repr(e)
+ response = {
+ 'result': None,
+ 'error': "InternalServerError: " + repr(e),
+ 'id': item['id']
+ }
+ txtResponse = json.dumps(response, self)
+
try:
- self.write(json.dumps(response, self))
+ self.write(txtResponse)
except TypeError:
print "response was:", repr(response)
raise

0 comments on commit 575b08b

Please sign in to comment.