commit 799ecdda86eddc59943fe10033a128071c965de0
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 13:56:26 2015 -0500
Error Handling chapter
- added example 26
- aded a new section to support example 26
en-US/Error_Handling.xml | 13 +++++++++++++
en-US/extras/Error-Example-26.py | 33 +++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/en-US/Error_Handling.xml b/en-US/Error_Handling.xml
index 79259b3..0601dfc 100644
--- a/en-US/Error_Handling.xml
+++ b/en-US/Error_Handling.xml
@@ -109,4 +109,17 @@
<programlisting language="Python"><xi:include
href="extras/Error-Example-25.py" parse="text"
xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
</example>
</section>
+
+ <section
id="libvirt_application_development_guide_using_python-Error_Handling-Registering_Error_Handler">
+ <title>Registering an Error Handler Function</title>
+ <para>
+ Libvirt also supports setting up an error handler function. This is done
using the libvirt
+ function <literal>registerErrorHandler</literal>.
+ An example of this follows:
+ </para>
+ <example>
+ <title>Subclassing virError</title>
+ <programlisting language="Python"><xi:include
href="extras/Error-Example-26.py" parse="text"
xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
+ </example>
+ </section>
</chapter>
diff --git a/en-US/extras/Error-Example-26.py b/en-US/extras/Error-Example-26.py
new file mode 100644
index 0000000..a8bf911
--- /dev/null
+++ b/en-US/extras/Error-Example-26.py
@@ -0,0 +1,33 @@
+# Example-26.py
+from __future__ import print_function
+import sys
+import libvirt
+
+def libvirt_error_handler(ctx, err):
+ print('Error code: '+str(err[0]), file=sys.stderr)
+ print('Error domain: '+str(err[1]), file=sys.stderr)
+ print('Error message: '+err[2], file=sys.stderr)
+ print('Error level: '+str(err[3]), file=sys.stderr)
+ if err[4] != None:
+ print('Error string1: '+err[4], file=sys.stderr)
+ else:
+ print('Error string1:', file=sys.stderr)
+ if err[5] != None:
+ print('Error string2: '+err[5], file=sys.stderr)
+ else:
+ print('Error string2:', file=sys.stderr)
+ if err[6] != None:
+ print('Error string3: '+err[6], file=sys.stderr)
+ else:
+ print('Error string3:', file=sys.stderr)
+ print('Error int1: '+str(err[7]), file=sys.stderr)
+ print('Error int2: '+str(err[8]), file=sys.stderr)
+ exit(1)
+
+ctx = 'just some information'
+libvirt.registerErrorHandler(libvirt_error_handler, ctx)
+
+conn = libvirt.open('qemu:///system') # invalidate the parameter to force an
error
+
+conn.close()
+exit(0)