Blog Viewer

Scripting How-To: Handle Python exceptions

By Erdem posted 08-05-2015 19:36

  

Handle Python Exceptions

 

You can handle Python exceptions when things go wrong.


Specific exception names are imported as shown:

 

from jnpr.junos.exception import *

 The following exceptions are defined:

 

  • RpcError - general RPC error
  • CommitError - resulting for issuing a commit operation
  • LockError - resulting from issuing a lock-configuration operation
  • UnlockError - resulting from issuing an unlock-configuration command

All exceptions provide the following property attributes:

 

  • cmd - XML RPC command, lxml Element
  • rsp - XML RPC response, lxml Element

Generally speaking, if you have any doubt that an RPC will execute properly, you should trap the exception. You can either use the explicit RpcError exception or the generic Exception (shown):

 

try:   jdev.rpc.do_something_goofy(var1="dude")  # will result in RpcError exception
except Exception as err:   # now you can access err as an RpcError   print "CMD:"   etree.dump(err.cmd)   print "RSP:"   etree.dump(err.rsp)
# results in the following output:
# CMD:
# <do-something-goofy xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
#   <var1>dude</var1>
# </do-something-goofy>
# RSP:
# <rpc-error xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
#   mlns:junos=" http://xml.juniper.net/junos/12.1X44/junos" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
# <error-severity>error</error-severity>
# <error-info>
# <bad-element>do-something-goofy</bad-element>
# </error-info>
# <error-message>syntax error</error-message>
# </rpc-error>

 

The CommitError, LockError, and UnlockError inherit from RpcError, and are only raised when you use the Configutilities. If you perform a native RPC command directly, you will not get one of these Exceptions, but rather an exception from the underlying NETCONF transport module (ncclient as of now).

 

from jnpr.junos import Device
from jnpr.junos.utils import Config
from jnpr.junos.exception import *
# assume :dev: is an open Device instance
cu = Config(dev)
# assume that someone else has the exclusive configuration lock.
# the following will cause a LockError
cu.lock()
#>>> jnpr.junos.exception.LockError
# trying to do this with the native XML RPC results in an ncclient exception:
dev.rpc.lock_configuration()
#>>> ncclient.operations.rpc.RPCError:
#>>> Configuration database is already open

#overview
#Python
#junospyez
#How-To

Permalink