pydbus
pydbus copied to clipboard
Doc or examples don't include handling of dbus method errors
In Dbus methods can return errors instead of returning expected value. I could not find simple example or good documentation on how to handle them. A simple example showing how to handle common errors should be provided, as well as adding a paragraph in the documentation/tutorial.
For instance, from ofono https://git.kernel.org/pub/scm/network/ofono/ofono.git/tree/doc/network-api.txt:
void Register()
Attempts to register to the default network. The
default network is normally selected by the settings
from the SIM card.
Possible Errors: [service].Error.InProgress
[service].Error.NotImplemented
[service].Error.Failed
[service].Error.AccessDenied
And I got this stack trace today:
dbus_op.Register()
File "/usr/local/lib/python3.4/dist-packages/pydbus/proxy_method.py", line 75, in __call__
0, timeout_to_glib(timeout), None).unpack()
GLib.Error: g-io-error-quark: GDBus.Error:org.ofono.Error.InProgress: Operation already in progress (36)
For now I'll try to do something like this, but it's still not clear to me whether this is correct or not:
try:
netreg.Register()
except org.ofono.Error.InProgress as e:
self.log('Register already in progress', e)
For reference, I'm finally doing something like this now. Still not checked it's correct as I cannot reproduce the exception:
try:
method()
except Exception as e:
if isinstance(e, GLib.Error) and err_str in e.domain:
return
raise e
I ended up with:
from gi.repository.GLib import GError
...
try:
thing()
except GError as e:
if 'dbus.Error.Name' in str(e.args):
print('Deal with this error')
else:
raise e
still learning python, so not sure if that's right, but it is handling the exception.
This is probably useful as reference: https://pygobject.readthedocs.io/en/latest/guide/api/error_handling.html