Change client protocol to raise exception on failures
authorIustin Pop <iustin@google.com>
Thu, 10 Apr 2008 15:46:44 +0000 (15:46 +0000)
committerIustin Pop <iustin@google.com>
Thu, 10 Apr 2008 15:46:44 +0000 (15:46 +0000)
Currently the luxi.client.SubmitJob and Query methods return the unserialized
result without processing it at all. This patch changes this by adding a
'RequestException' error that is raised if the query itself or the
submission of the job failed, and (if not) returning only the 'result'
field from the message.

The patch also does processing on the result of a query if we queried
for jobs, as the 'op_list' field in the result has serialized opcodes
and we need the de-serialized.

Reviewed-by: ultrotter

lib/luxi.py

index 5944a43..df9ed89 100644 (file)
@@ -68,6 +68,19 @@ class DecodingError(ProtocolError):
   """Decoding failure on the receiving side"""
 
 
+class RequestError(ProtocolError):
+  """Error on request
+
+  This signifies an error in the request format or request handling,
+  but not (e.g.) an error in starting up an instance.
+
+  Some common conditions that can trigger this exception:
+    - job submission failed because the job data was wrong
+    - query failed because required fields were missing
+
+  """
+
+
 def SerializeJob(job):
   """Convert a job description to a string format.
 
@@ -252,8 +265,21 @@ class Client(object):
 
   def SubmitJob(self, job):
     """Submit a job"""
-    return self.SendRequest(REQ_SUBMIT, SerializeJob(job))
+    result = self.SendRequest(REQ_SUBMIT, SerializeJob(job))
+    if not result['success']:
+      raise RequestError(result['result'])
+    return result['result']
 
   def Query(self, data):
     """Make a query"""
-    return self.SendRequest(REQ_QUERY, data)
+    result = self.SendRequest(REQ_QUERY, data)
+    if not result['success']:
+      raise RequestError(result[result])
+    result = result['result']
+    if data["object"] == "jobs":
+      # custom job processing of query values
+      for row in result:
+        for idx, field in enumerate(data["fields"]):
+          if field == "op_list":
+            row[idx] = [opcodes.OpCode.LoadOpCode(i) for i in row[idx]]
+    return result