[Libguestfs] [PATCH libnbd v2] python: Raise a custom exception containing error string and errno.

Eric Blake eblake at redhat.com
Fri Jun 28 16:49:19 UTC 2019


On 6/28/19 7:31 AM, Richard W.M. Jones wrote:
> This kind of fixes the problems in v1.  The exception still primarily
> lives in the libnbdmod and you could still refer to it using
> libnbdmod.Error (but don't do that).  However when the exception is
> printed it now appears as nbd.Error, and you can catch it also using
> the same name.
> 
> Other problems:
> 
>  - There is no "nice" interface to accessing the exception fields.
>    You have to use ex.args[0] and ex.args[1].

I think we can make it nicer with a bit of effort - we just need to
figure out how to tie in an __init__ method that parses the args tuple
and assigns direct attributes (the args tuple will always be there,
since we inherit that from the Exception base class, but adding our own
local attributes makes access easier).  We may also want a __str__
method for sanely printing the contents of our exception.

> 
>  - The errno is represented as a number, which means it will be hard
>    to write portable Python code depending on the errno.

Not that difficult. The standard python error module can doe reverse
lookup error.errorcode[integer] to give you the string 'EINVAL' (if
integer has the same value as errno.EINVAL).

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20190628/968e6053/attachment.sig>


More information about the Libguestfs mailing list