Note: the functionality specified in this document is currently subject to some change. Feedback as to useful functionality is appreciated. Please open an issue.
PyangBind re-uses Python error types where possible, particularly:
KeyErrorwill be raised when an element does not have a particular key. The arguments to this error are a string.
ValueErroris raised when the supplied data does not match the YANG data type. This value is only raised where the input cannot be cast to the type that is stored in the data. For example, a YANG integer type (
int16, … etc.) will accept
Trueas an input but store the value
1. This intentional and aims to provide a balance between duck-typing in Python, and the more strict typing in YANG:
- The argument passed by PyangBind setters to ValueError is a dictionary - this can be accessed using code such as:
1try: 2 pybindobj.value = "anInvalidValue" 3except ValueError as e: 4 # Check the args and types, just in case we have 5 # old bindings 6 if len(e.args) and isinstance(e.args, dict): 7 print "Hit a PyangBind ValueError" 8 for k, v in e.args.iteritems(): 9 print "%s->%s" % (k, v) 10 else: 11 print unicode(e)
- The keys of the dictionary are:
error-string: a simple error string that provides some insight (but not all information) as to the type that was not matched. It will currently capture the original YANG type that was specified, but no additional restrictions.
generated-type: the dynamic class specification that PyangBind tried to generate for this type - this is often unwieldy, but tends to be useful for debugging.
defined-type: the simple defined type, resolved to module if it is not native that the leaf is. Again this does not include all information.
AttributeErrorwill be raised when an invalid member of a YANG object is specified, or a method does not exist. In some cases, since PyangBind’s meta-class defines some methods which are used to modify mutable objects in place (to capture changes) then
dir(...)for the object may show methods that return
AttributeErrorwhen they are passed to the super-class.