Revision d3c8b360 lib/rpc.py
b/lib/rpc.py | ||
---|---|---|
85 | 85 |
|
86 | 86 |
@ivar data: the data payload, for successfull results, or None |
87 | 87 |
@type failed: boolean |
88 |
@ivar failed: whether the operation failed at RPC level (not
|
|
88 |
@ivar failed: whether the operation failed at transport level (not
|
|
89 | 89 |
application level on the remote node) |
90 | 90 |
@ivar call: the name of the RPC call |
91 | 91 |
@ivar node: the name of the node to which we made the call |
... | ... | |
93 | 93 |
offline, as opposed to actual failure; offline=True will always |
94 | 94 |
imply failed=True, in order to allow simpler checking if |
95 | 95 |
the user doesn't care about the exact failure mode |
96 |
@ivar error: the error message if the call failed |
|
96 | 97 |
|
97 | 98 |
""" |
98 | 99 |
def __init__(self, data=None, failed=False, offline=False, |
... | ... | |
106 | 107 |
self.error = "Node is marked offline" |
107 | 108 |
self.data = self.payload = None |
108 | 109 |
elif failed: |
109 |
self.error = data
|
|
110 |
self.error = self._EnsureErr(data)
|
|
110 | 111 |
self.data = self.payload = None |
111 | 112 |
else: |
112 | 113 |
self.data = data |
113 |
self.error = None |
|
114 |
if isinstance(data, (tuple, list)) and len(data) == 2: |
|
115 |
self.payload = data[1] |
|
114 |
if not isinstance(self.data, (tuple, list)): |
|
115 |
self.error = ("RPC layer error: invalid result type (%s)" % |
|
116 |
type(self.data)) |
|
117 |
elif len(data) != 2: |
|
118 |
self.error = ("RPC layer error: invalid result length (%d), " |
|
119 |
"expected 2" % len(self.data)) |
|
120 |
elif not self.data[0]: |
|
121 |
self.error = self._EnsureErr(self.data[1]) |
|
116 | 122 |
else: |
117 |
self.payload = None |
|
123 |
# finally success |
|
124 |
self.error = None |
|
125 |
self.payload = data[1] |
|
126 |
|
|
127 |
@staticmethod |
|
128 |
def _EnsureErr(val): |
|
129 |
"""Helper to ensure we return a 'True' value for error.""" |
|
130 |
if val: |
|
131 |
return val |
|
132 |
else: |
|
133 |
return "No error information" |
|
118 | 134 |
|
119 | 135 |
def Raise(self): |
120 | 136 |
"""If the result has failed, raise an OpExecError. |
... | ... | |
130 | 146 |
def RemoteFailMsg(self): |
131 | 147 |
"""Check if the remote procedure failed. |
132 | 148 |
|
133 |
This is valid only for RPC calls which return result of the form |
|
134 |
(status, data | error_msg). |
|
135 |
|
|
136 |
@return: empty string for succcess, otherwise an error message |
|
149 |
@return: the fail_msg attribute |
|
137 | 150 |
|
138 | 151 |
""" |
139 |
def _EnsureErr(val): |
|
140 |
"""Helper to ensure we return a 'True' value for error.""" |
|
141 |
if val: |
|
142 |
return val |
|
143 |
else: |
|
144 |
return "No error information" |
|
145 |
|
|
146 |
if self.failed: |
|
147 |
return _EnsureErr(self.error) |
|
148 |
if not isinstance(self.data, (tuple, list)): |
|
149 |
return "Invalid result type (%s)" % type(self.data) |
|
150 |
if len(self.data) != 2: |
|
151 |
return "Invalid result length (%d), expected 2" % len(self.data) |
|
152 |
if not self.data[0]: |
|
153 |
return _EnsureErr(self.data[1]) |
|
154 |
return "" |
|
152 |
return self.error |
|
155 | 153 |
|
156 | 154 |
|
157 | 155 |
class Client: |
Also available in: Unified diff