mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-11 19:54:05 +02:00
.gitlab-ci
.reuse
LICENSES
docs
reference
gio
dbus-error.md
dbus-introspection.md
dbus-name-owning.md
dbus-name-watching.md
dbus-utils.md
error.md
file-attributes.md
gapplication.rst
gdbus-codegen.rst
gdbus.rst
gio-querymodules.rst
gio-unix.toml.in
gio-win32.toml.in
gio.rst
gio.toml.in
glib-compile-resources.rst
glib-compile-schemas.rst
gresource.rst
gsettings.rst
gvfs-overview.odg
gvfs-overview.png
io-scheduler.md
menu-example.png
menu-exporter.md
menu-model.png
meson.build
migrating-gconf.md
migrating-gdbus.md
migrating-gnome-vfs.md
migrating-posix.md
networking.md
overview.md
pollable-utils.md
tls-overview.md
unix-mounts.md
girepository
glib
gmodule
gobject
AUTHORS
COPYING
NEWS
meson.build
urlmap.js
CODEOWNERS
backports.md
g-ir-compiler.1
g-ir-generate.1
issue-and-merge-request-management.md
macros.md
meson-version.md
rationales.md
releasing.md
roadmap.md
supported-platforms.md
testing.md
toolchain-requirements.md
win32-build.md
fuzzing
gio
girepository
glib
gmodule
gobject
gthread
m4macros
po
subprojects
tests
tools
.clang-format
.dir-locals.el
.editorconfig
.gitignore
.gitlab-ci.yml
.gitmodules
.lcovrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
COPYING
INSTALL.md
NEWS
README.md
SECURITY.md
glib.doap
meson.build
meson_options.txt
80 lines
3.0 KiB
Markdown
80 lines
3.0 KiB
Markdown
![]() |
Title: D-Bus Error Handling
|
||
|
SPDX-License-Identifier: LGPL-2.1-or-later
|
||
|
SPDX-FileCopyrightText: 2010 David Zeuthen
|
||
|
SPDX-FileCopyrightText: 2012 Aleksander Morgado
|
||
|
|
||
|
# D-Bus Error Handling
|
||
|
|
||
|
All facilities that return errors from remote methods (such as
|
||
|
[method@Gio.DBusConnection.call_sync]) use [type@GLib.Error] to represent both
|
||
|
D-Bus errors (e.g. errors returned from the other peer) and locally in-process
|
||
|
generated errors.
|
||
|
|
||
|
To check if a returned [type@GLib.Error] is an error from a remote peer, use
|
||
|
[func@Gio.DBusError.is_remote_error]. To get the actual D-Bus error name,
|
||
|
use [func@Gio.DBusError.get_remote_error]. Before presenting an error, always
|
||
|
use [func@Gio.DBusError.strip_remote_error].
|
||
|
|
||
|
In addition, facilities used to return errors to a remote peer also use
|
||
|
[type@GLib.Error]. See [method@Gio.DBusMethodInvocation.return_error] for
|
||
|
discussion about how the D-Bus error name is set.
|
||
|
|
||
|
Applications can associate a [type@GLib.Error] error domain with a set of D-Bus
|
||
|
errors in order to automatically map from D-Bus errors to [type@GLib.Error] and
|
||
|
back. This is typically done in the function returning the [type@GLib.Quark] for
|
||
|
the error domain:
|
||
|
|
||
|
```c
|
||
|
// foo-bar-error.h:
|
||
|
|
||
|
#define FOO_BAR_ERROR (foo_bar_error_quark ())
|
||
|
GQuark foo_bar_error_quark (void);
|
||
|
|
||
|
typedef enum
|
||
|
{
|
||
|
FOO_BAR_ERROR_FAILED,
|
||
|
FOO_BAR_ERROR_ANOTHER_ERROR,
|
||
|
FOO_BAR_ERROR_SOME_THIRD_ERROR,
|
||
|
FOO_BAR_N_ERRORS / *< skip >* /
|
||
|
} FooBarError;
|
||
|
|
||
|
// foo-bar-error.c:
|
||
|
|
||
|
static const GDBusErrorEntry foo_bar_error_entries[] =
|
||
|
{
|
||
|
{FOO_BAR_ERROR_FAILED, "org.project.Foo.Bar.Error.Failed"},
|
||
|
{FOO_BAR_ERROR_ANOTHER_ERROR, "org.project.Foo.Bar.Error.AnotherError"},
|
||
|
{FOO_BAR_ERROR_SOME_THIRD_ERROR, "org.project.Foo.Bar.Error.SomeThirdError"},
|
||
|
};
|
||
|
|
||
|
// Ensure that every error code has an associated D-Bus error name
|
||
|
G_STATIC_ASSERT (G_N_ELEMENTS (foo_bar_error_entries) == FOO_BAR_N_ERRORS);
|
||
|
|
||
|
GQuark
|
||
|
foo_bar_error_quark (void)
|
||
|
{
|
||
|
static gsize quark = 0;
|
||
|
g_dbus_error_register_error_domain ("foo-bar-error-quark",
|
||
|
&quark,
|
||
|
foo_bar_error_entries,
|
||
|
G_N_ELEMENTS (foo_bar_error_entries));
|
||
|
return (GQuark) quark;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
With this setup, a D-Bus peer can transparently pass e.g.
|
||
|
`FOO_BAR_ERROR_ANOTHER_ERROR` and other peers will see the D-Bus error name
|
||
|
`org.project.Foo.Bar.Error.AnotherError`.
|
||
|
|
||
|
If the other peer is using GDBus, and has registered the association with
|
||
|
[func@Gio.DBusError.register_error_domain] in advance (e.g. by invoking the
|
||
|
`FOO_BAR_ERROR` quark generation itself in the previous example) the peer will
|
||
|
see also `FOO_BAR_ERROR_ANOTHER_ERROR` instead of `G_IO_ERROR_DBUS_ERROR`. Note
|
||
|
that GDBus clients can still recover `org.project.Foo.Bar.Error.AnotherError`
|
||
|
using [func@Gio.DBusError.get_remote_error].
|
||
|
|
||
|
Note that the `G_DBUS_ERROR` error domain is intended only for returning errors
|
||
|
from a remote message bus process. Errors generated locally in-process by e.g.
|
||
|
[class@Gio.DBusConnection] should use the `G_IO_ERROR` domain.
|
||
|
|