Skip to content

Commit d7298f8

Browse files
committed
Exception improvements
- Create an IloCommunicationError subclass - Add the ilo status message to the exception objects This makes it easier to trap specific errors. If only there was a list of possible errors.
1 parent c828889 commit d7298f8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

hpilo.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def untested(meth):
5353
return meth
5454

5555
class IloError(Exception):
56+
def __init__(self, message, errorcode=None):
57+
super(IloError, self).__init__(message)
58+
self.errorcode = error_code
59+
60+
class IloCommunicationError(IloError):
5661
pass
5762

5863
class IloLoginFailed(IloError):
@@ -201,7 +206,7 @@ def _upload_file(self, filename, progress):
201206
except socket.sslerror: # Connection closed
202207
e = sys.exc_info()[1]
203208
if not data:
204-
raise IloError("Communication with %s:%d failed: %s" % (self.hostname, self.port, str(e)))
209+
raise IloCommunicationError("Communication with %s:%d failed: %s" % (self.hostname, self.port, str(e)))
205210

206211
self._debug(1, "Received %d bytes" % len(data))
207212
self._debug(2, data)
@@ -216,7 +221,7 @@ def _get_socket(self):
216221
sp = subprocess.Popen([self.hponcfg, '--input', '--xmlverbose'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
217222
except OSError:
218223
e = sys.exc_info()[1]
219-
raise IloError("Cannot run %s: %s" % (self.hponcfg, str(e)))
224+
raise IloCommunicationError("Cannot run %s: %s" % (self.hponcfg, str(e)))
220225
sp.write = sp.stdin.write
221226
sp.read = sp.stdout.read
222227
return sp
@@ -227,15 +232,15 @@ def _get_socket(self):
227232
try:
228233
sock.connect((self.hostname, self.port))
229234
except socket.timeout:
230-
raise IloError("Timeout connecting to %s:%d" % (self.hostname, self.port))
235+
raise IloCommunicationError("Timeout connecting to %s:%d" % (self.hostname, self.port))
231236
except socket.error:
232237
e = sys.exc_info()[1]
233-
raise IloError("Error connecting to %s:%d: %s" % (self.hostname, self.port, str(e)))
238+
raise IloCommunicationError("Error connecting to %s:%d: %s" % (self.hostname, self.port, str(e)))
234239
try:
235240
return ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1)
236241
except socket.sslerror:
237242
e = sys.exc_info()[1]
238-
raise IloError("Cannot establish ssl session with %s:%d: %s" % (self.hostname, self.port, e.message or str(e)))
243+
raise IloCommunicationError("Cannot establish ssl session with %s:%d: %s" % (self.hostname, self.port, e.message or str(e)))
239244

240245
def _communicate(self, xml, protocol, progress=None):
241246
sock = self._get_socket()
@@ -303,7 +308,7 @@ def _communicate(self, xml, protocol, progress=None):
303308
except socket.sslerror: # Connection closed
304309
e = sys.exc_info()[1]
305310
if not data:
306-
raise IloError("Communication with %s:%d failed: %s" % (self.hostname, self.port, str(e)))
311+
raise IloCommunicationError("Communication with %s:%d failed: %s" % (self.hostname, self.port, str(e)))
307312

308313
self._debug(1, "Received %d bytes" % len(data))
309314

@@ -375,10 +380,12 @@ def _parse_message(self, data, include_inform=False):
375380
# This is triggered when doing protocol detection, ignore
376381
pass
377382
else:
378-
if int(child.get('STATUS'), 16) in IloLoginFailed.possible_codes or \
379-
child.get('MESSAGE') in IloLoginFailed.possible_messages:
380-
raise IloLoginFailed
381-
raise IloError("Error communicating with iLO: %s" % child.get('MESSAGE'))
383+
status = int(child.get('STATUS'), 16)
384+
message = child.get('MESSAGE')
385+
if status in IloLoginFailed.possible_codes or \
386+
message in IloLoginFailed.possible_messages:
387+
raise IloLoginFailed(message, status)
388+
raise IloError(message, status)
382389
# And this type of message is the actual payload.
383390
else:
384391
return message
@@ -555,7 +562,7 @@ def computer_lock_config(self, computer_lock=None, computer_lock_key=None):
555562
if computer_lock_key:
556563
computer_lock = "custom"
557564
if not computer_lock:
558-
raise IloError("A value must be specified for computer_lock")
565+
raise ValueError("A value must be specified for computer_lock")
559566
elements = [etree.Element('COMPUTER_LOCK', VALUE=computer_lock)]
560567
if computer_lock_key:
561568
elements.append(etree.Element('COMPUTER_LOCK_KEY', VALUE=computer_lock_key))

0 commit comments

Comments
 (0)