From 3247bbac3ba3839555f6ec654aace96d0d3d8041 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 8 Jan 2009 16:05:30 +0000
Subject: [PATCH] rpc: Add a method for easy check of remote results

The patch adds a new method to the rpc.RpcResult class called
"RemoteFailMsg" which is useful for the RPC calls which return a
(status, payload) style result.

Reviewed-by: imsnah
---
 lib/rpc.py | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/lib/rpc.py b/lib/rpc.py
index 10b48b1da..065da5984 100644
--- a/lib/rpc.py
+++ b/lib/rpc.py
@@ -123,6 +123,32 @@ class RpcResult(object):
       raise errors.OpExecError("Call '%s' to node '%s' has failed: %s" %
                                (self.call, self.node, self.error))
 
+  def RemoteFailMsg(self):
+    """Check if the remote procedure failed.
+
+    This is valid only for RPC calls which return result of the form
+    (status, data | error_msg).
+
+    @return: empty string for succcess, otherwise an error message
+
+    """
+    def _EnsureErr(val):
+      """Helper to ensure we return a 'True' value for error."""
+      if val:
+        return val
+      else:
+        return "No error information"
+
+    if self.failed:
+      return _EnsureErr(self.error)
+    if not isinstance(self.data, (tuple, list)):
+      return "Invalid result type (%s)" % type(self.data)
+    if len(self.data) != 2:
+      return "Invalid result length (%d), expected 2" % len(self.data)
+    if not self.data[0]:
+      return _EnsureErr(self.data[1])
+    return ""
+
 
 class Client:
   """RPC Client class.
-- 
GitLab