Forum Discussion

Michael_57183's avatar
Michael_57183
Icon for Nimbostratus rankNimbostratus
Feb 24, 2012

Handling exceptions in pyControl

Hi,

Can someone please clarify how to best handle exceptions in pycontrol?

I'm writing a python script to change the monitor state of a given pool member using LocalLB::Pool::set_member_monitor_state. When set_member_monitor_state() is called with an AddressPort that is not actually in the given pool, it causes a Common::OperationFailed exception such as:

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
...
  File "/usr/local/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 542, in __call__
  File "/usr/local/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 602, in invoke
  File "/usr/local/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 649, in send
  File "/usr/local/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/client.py", line 702, in failed
  File "/usr/local/lib/python2.6/site-packages/suds-0.4-py2.6.egg/suds/bindings/binding.py", line 265, in get_fault
suds.WebFault: Server raised fault: 'Exception caught in LocalLB::urn:iControl:LocalLB/Pool::set_member_monitor_state()
Exception: Common::OperationFailed
primary_error_code   : 16908342 (0x01020036)
secondary_error_code : 0
error_string         : 01020036:3: The requested pool member (/Common/FR-R-RTL-FRM /Common/FR-R-RTL2 8080) was not found.'

What I want to do is catch the exception if and only if it was raised because "the requested pool member...was not found" (side question: is the primary_error_code equal to 16908342 only when the pool member is not found in the pool, or is it also something more general?). However, the exception name itself is very general (OperationFailed), and the argument to the exception appears to be just a really long string. ie:

try:
b.LocalLB.Pool.set_member_monitor_state(
 pool_names = [pool],
 members = member_seqseq,
 monitor_states = state_seqseq
)
except Exception, e:
print e.args

...produces the following output:

No handlers could be found for logger "suds.client"
(u"Server raised fault: 'Exception caught in LocalLB::urn:iControl:LocalLB/Pool::set_member_monitor_state()\nException: Common::OperationFailed\n\tprimary_error_code   : 16908342 (0x01020036)\n\tsecondary_error_code : 0\n\terror_string         : 01020036:3: The requested pool member (/Common/MYPOOL /Common/MYHOST 8080) was not found.'",)

I figure I *could* parse e.args using a regex for either /error_code: ([0-9])*/ or /error_string: (.)*/, but I figure there is a more elegant solution.

Is there a "best practice" recommendation on how to handle specific iControl exceptions?

Thanks,

Michael

  • I asked this question also previously. The best way I've found is to actually just parse the text ("e") and handle appropriately.