GNOME Bugzilla – Bug 662396
Need to register errors before translating errors received on the wire
Last modified: 2018-05-24 13:28:28 UTC
I have followed the instructions at http://developer.gnome.org/gio/2.29/GDBusError.html#error-registration I use GDBus to make a method call, which returns an error like org.project.Foo.Bar.Error.Failed. I inspect the resulting GError, and check for error->domain == FOO_BAR_ERROR && error->code == FOO_BAR_ERROR_FAILED. But the comparison *fails*, the first time I do this. This is because my *comparison* is the first time that FOO_BAR_ERROR is evaluated and the foo_bar_error_quark() function is called. The errors weren't *registered* for translation until the first comparison. By adding a "dummy" call to foo_bar_error_quark() in my code before making the GDBus call, I can make things work properly. I can't see a nice way to *fix* this without C++ initialisers, but at least we could document it properly. The above-referenced documentation says nothing about this issue.
(In reply to comment #0) > I can't see a nice way to *fix* this without C++ initialisers, GCC has _attribute ((constructor)) and other compilers / binary formats has similar features. See e.g. http://people.freedesktop.org/~david/c++-like-constructors-in-c.c.txt ... Other (better?) approaches include registering the errors - in the get_gtype(), init() or class_init() function for "anchor/context" types (e.g. a type you know users of your library will instantiate ... for example GUdevClient or GVolumeManager); or - if your library has an init function, do it there (but I generally recommend against init() functions) - in GDBusProxy-derived classes ... for example, we could add a feature to gdbus-codegen(1) so it will call one or more init() functions of your choice before doing any method calls But, yeah, it's generally a hard problem. > but at least we > could document it properly. The above-referenced documentation says nothing > about this issue. It's sorta implicit in the docs here http://developer.gnome.org/gio/unstable/gio-GDBusError.html#g-dbus-error-new-for-dbus-error But, sure, I don't mind adding a comment explicitly stating this is needed and the four approaches above.
Somewhat related: bug 662693 comment 1 for a proposal to make gdbus-codegen(1) generate GError error domains for D-Bus errors.
Updating summary to be more accurate.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/469.