1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbuserror.h"
30 #include "gioenumtypes.h"
32 #include "gdbusprivate.h"
39 * @short_description: Mapping D-Bus errors to and from GError
42 * All facilities that return errors from remote methods (such as
43 * g_dbus_connection_call_sync()) use #GError to represent both D-Bus
44 * errors (e.g. errors returned from the other peer) and locally
45 * in-process generated errors.
47 * To check if a returned #GError is an error from a remote peer, use
48 * g_dbus_error_is_remote_error(). To get the actual D-Bus error name,
49 * use g_dbus_error_get_remote_error(). Before presenting an error,
50 * always use g_dbus_error_strip_remote_error().
52 * In addition, facilities used to return errors to a remote peer also
53 * use #GError. See g_dbus_method_invocation_return_error() for
54 * discussion about how the D-Bus error name is set.
56 * Applications can associate a #GError error domain with a set of D-Bus errors in order to
57 * automatically map from D-Bus errors to #GError and back. This
58 * is typically done in the function returning the #GQuark for the
60 * |[<!-- language="C" -->
63 * #define FOO_BAR_ERROR (foo_bar_error_quark ())
64 * GQuark foo_bar_error_quark (void);
68 * FOO_BAR_ERROR_FAILED,
69 * FOO_BAR_ERROR_ANOTHER_ERROR,
70 * FOO_BAR_ERROR_SOME_THIRD_ERROR,
71 * FOO_BAR_N_ERRORS / *< skip >* /
76 * static const GDBusErrorEntry foo_bar_error_entries[] =
78 * {FOO_BAR_ERROR_FAILED, "org.project.Foo.Bar.Error.Failed"},
79 * {FOO_BAR_ERROR_ANOTHER_ERROR, "org.project.Foo.Bar.Error.AnotherError"},
80 * {FOO_BAR_ERROR_SOME_THIRD_ERROR, "org.project.Foo.Bar.Error.SomeThirdError"},
83 * // Ensure that every error code has an associated D-Bus error name
84 * G_STATIC_ASSERT (G_N_ELEMENTS (foo_bar_error_entries) == FOO_BAR_N_ERRORS);
87 * foo_bar_error_quark (void)
89 * static gsize quark = 0;
90 * g_dbus_error_register_error_domain ("foo-bar-error-quark",
92 * foo_bar_error_entries,
93 * G_N_ELEMENTS (foo_bar_error_entries));
94 * return (GQuark) quark;
97 * With this setup, a D-Bus peer can transparently pass e.g. %FOO_BAR_ERROR_ANOTHER_ERROR and
98 * other peers will see the D-Bus error name org.project.Foo.Bar.Error.AnotherError.
100 * If the other peer is using GDBus, and has registered the association with
101 * g_dbus_error_register_error_domain() in advance (e.g. by invoking the %FOO_BAR_ERROR quark
102 * generation itself in the previous example) the peer will see also %FOO_BAR_ERROR_ANOTHER_ERROR instead
103 * of %G_IO_ERROR_DBUS_ERROR. Note that GDBus clients can still recover
104 * org.project.Foo.Bar.Error.AnotherError using g_dbus_error_get_remote_error().
106 * Note that the %G_DBUS_ERROR error domain is intended only
107 * for returning errors from a remote message bus process. Errors
108 * generated locally in-process by e.g. #GDBusConnection should use the
109 * %G_IO_ERROR domain.
112 static const GDBusErrorEntry g_dbus_error_entries[] =
114 {G_DBUS_ERROR_FAILED, "org.freedesktop.DBus.Error.Failed"},
115 {G_DBUS_ERROR_NO_MEMORY, "org.freedesktop.DBus.Error.NoMemory"},
116 {G_DBUS_ERROR_SERVICE_UNKNOWN, "org.freedesktop.DBus.Error.ServiceUnknown"},
117 {G_DBUS_ERROR_NAME_HAS_NO_OWNER, "org.freedesktop.DBus.Error.NameHasNoOwner"},
118 {G_DBUS_ERROR_NO_REPLY, "org.freedesktop.DBus.Error.NoReply"},
119 {G_DBUS_ERROR_IO_ERROR, "org.freedesktop.DBus.Error.IOError"},
120 {G_DBUS_ERROR_BAD_ADDRESS, "org.freedesktop.DBus.Error.BadAddress"},
121 {G_DBUS_ERROR_NOT_SUPPORTED, "org.freedesktop.DBus.Error.NotSupported"},
122 {G_DBUS_ERROR_LIMITS_EXCEEDED, "org.freedesktop.DBus.Error.LimitsExceeded"},
123 {G_DBUS_ERROR_ACCESS_DENIED, "org.freedesktop.DBus.Error.AccessDenied"},
124 {G_DBUS_ERROR_AUTH_FAILED, "org.freedesktop.DBus.Error.AuthFailed"},
125 {G_DBUS_ERROR_NO_SERVER, "org.freedesktop.DBus.Error.NoServer"},
126 {G_DBUS_ERROR_TIMEOUT, "org.freedesktop.DBus.Error.Timeout"},
127 {G_DBUS_ERROR_NO_NETWORK, "org.freedesktop.DBus.Error.NoNetwork"},
128 {G_DBUS_ERROR_ADDRESS_IN_USE, "org.freedesktop.DBus.Error.AddressInUse"},
129 {G_DBUS_ERROR_DISCONNECTED, "org.freedesktop.DBus.Error.Disconnected"},
130 {G_DBUS_ERROR_INVALID_ARGS, "org.freedesktop.DBus.Error.InvalidArgs"},
131 {G_DBUS_ERROR_FILE_NOT_FOUND, "org.freedesktop.DBus.Error.FileNotFound"},
132 {G_DBUS_ERROR_FILE_EXISTS, "org.freedesktop.DBus.Error.FileExists"},
133 {G_DBUS_ERROR_UNKNOWN_METHOD, "org.freedesktop.DBus.Error.UnknownMethod"},
134 {G_DBUS_ERROR_TIMED_OUT, "org.freedesktop.DBus.Error.TimedOut"},
135 {G_DBUS_ERROR_MATCH_RULE_NOT_FOUND, "org.freedesktop.DBus.Error.MatchRuleNotFound"},
136 {G_DBUS_ERROR_MATCH_RULE_INVALID, "org.freedesktop.DBus.Error.MatchRuleInvalid"},
137 {G_DBUS_ERROR_SPAWN_EXEC_FAILED, "org.freedesktop.DBus.Error.Spawn.ExecFailed"},
138 {G_DBUS_ERROR_SPAWN_FORK_FAILED, "org.freedesktop.DBus.Error.Spawn.ForkFailed"},
139 {G_DBUS_ERROR_SPAWN_CHILD_EXITED, "org.freedesktop.DBus.Error.Spawn.ChildExited"},
140 {G_DBUS_ERROR_SPAWN_CHILD_SIGNALED, "org.freedesktop.DBus.Error.Spawn.ChildSignaled"},
141 {G_DBUS_ERROR_SPAWN_FAILED, "org.freedesktop.DBus.Error.Spawn.Failed"},
142 {G_DBUS_ERROR_SPAWN_SETUP_FAILED, "org.freedesktop.DBus.Error.Spawn.FailedToSetup"},
143 {G_DBUS_ERROR_SPAWN_CONFIG_INVALID, "org.freedesktop.DBus.Error.Spawn.ConfigInvalid"},
144 {G_DBUS_ERROR_SPAWN_SERVICE_INVALID, "org.freedesktop.DBus.Error.Spawn.ServiceNotValid"},
145 {G_DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND, "org.freedesktop.DBus.Error.Spawn.ServiceNotFound"},
146 {G_DBUS_ERROR_SPAWN_PERMISSIONS_INVALID, "org.freedesktop.DBus.Error.Spawn.PermissionsInvalid"},
147 {G_DBUS_ERROR_SPAWN_FILE_INVALID, "org.freedesktop.DBus.Error.Spawn.FileInvalid"},
148 {G_DBUS_ERROR_SPAWN_NO_MEMORY, "org.freedesktop.DBus.Error.Spawn.NoMemory"},
149 {G_DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "org.freedesktop.DBus.Error.UnixProcessIdUnknown"},
150 {G_DBUS_ERROR_INVALID_SIGNATURE, "org.freedesktop.DBus.Error.InvalidSignature"},
151 {G_DBUS_ERROR_INVALID_FILE_CONTENT, "org.freedesktop.DBus.Error.InvalidFileContent"},
152 {G_DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"},
153 {G_DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN, "org.freedesktop.DBus.Error.AdtAuditDataUnknown"},
154 {G_DBUS_ERROR_OBJECT_PATH_IN_USE, "org.freedesktop.DBus.Error.ObjectPathInUse"},
155 {G_DBUS_ERROR_UNKNOWN_OBJECT, "org.freedesktop.DBus.Error.UnknownObject"},
156 {G_DBUS_ERROR_UNKNOWN_INTERFACE, "org.freedesktop.DBus.Error.UnknownInterface"},
157 {G_DBUS_ERROR_UNKNOWN_PROPERTY, "org.freedesktop.DBus.Error.UnknownProperty"},
158 {G_DBUS_ERROR_PROPERTY_READ_ONLY, "org.freedesktop.DBus.Error.PropertyReadOnly"},
162 g_dbus_error_quark (void)
164 G_STATIC_ASSERT (G_N_ELEMENTS (g_dbus_error_entries) - 1 == G_DBUS_ERROR_PROPERTY_READ_ONLY);
165 static gsize quark = 0;
166 g_dbus_error_register_error_domain ("g-dbus-error-quark",
168 g_dbus_error_entries,
169 G_N_ELEMENTS (g_dbus_error_entries));
170 return (GQuark) quark;
174 * g_dbus_error_register_error_domain:
175 * @error_domain_quark_name: The error domain name.
176 * @quark_volatile: A pointer where to store the #GQuark.
177 * @entries: (array length=num_entries): A pointer to @num_entries #GDBusErrorEntry struct items.
178 * @num_entries: Number of items to register.
180 * Helper function for associating a #GError error domain with D-Bus error names.
182 * While @quark_volatile has a `volatile` qualifier, this is a historical
183 * artifact and the argument passed to it should not be `volatile`.
188 g_dbus_error_register_error_domain (const gchar *error_domain_quark_name,
189 volatile gsize *quark_volatile,
190 const GDBusErrorEntry *entries,
195 g_return_if_fail (error_domain_quark_name != NULL);
196 g_return_if_fail (quark_volatile != NULL);
197 g_return_if_fail (entries != NULL);
198 g_return_if_fail (num_entries > 0);
200 /* Drop the volatile qualifier, which should never have been on the argument
201 * in the first place. */
202 quark = (gsize *) quark_volatile;
204 if (g_once_init_enter (quark))
209 new_quark = g_quark_from_static_string (error_domain_quark_name);
211 for (n = 0; n < num_entries; n++)
213 g_warn_if_fail (g_dbus_error_register_error (new_quark,
214 entries[n].error_code,
215 entries[n].dbus_error_name));
217 g_once_init_leave (quark, new_quark);
222 _g_dbus_error_decode_gerror (const gchar *dbus_name,
223 GQuark *out_error_domain,
224 gint *out_error_code)
229 gchar *domain_quark_string;
234 if (g_str_has_prefix (dbus_name, "org.gtk.GDBus.UnmappedGError.Quark._"))
236 s = g_string_new (NULL);
238 for (n = sizeof "org.gtk.GDBus.UnmappedGError.Quark._" - 1;
239 dbus_name[n] != '.' && dbus_name[n] != '\0';
242 if (g_ascii_isalnum (dbus_name[n]))
244 g_string_append_c (s, dbus_name[n]);
246 else if (dbus_name[n] == '_')
253 nibble_top = dbus_name[n];
254 if (nibble_top >= '0' && nibble_top <= '9')
256 else if (nibble_top >= 'a' && nibble_top <= 'f')
257 nibble_top -= ('a' - 10);
263 nibble_bottom = dbus_name[n];
264 if (nibble_bottom >= '0' && nibble_bottom <= '9')
265 nibble_bottom -= '0';
266 else if (nibble_bottom >= 'a' && nibble_bottom <= 'f')
267 nibble_bottom -= ('a' - 10);
271 g_string_append_c (s, (nibble_top<<4) | nibble_bottom);
279 if (!g_str_has_prefix (dbus_name + n, ".Code"))
282 domain_quark_string = g_string_free (s, FALSE);
285 if (out_error_domain != NULL)
286 *out_error_domain = g_quark_from_string (domain_quark_string);
287 g_free (domain_quark_string);
289 if (out_error_code != NULL)
290 *out_error_code = atoi (dbus_name + n + sizeof ".Code" - 1);
298 g_string_free (s, TRUE);
303 /* ---------------------------------------------------------------------------------------------------- */
312 quark_code_pair_hash_func (const QuarkCodePair *pair)
315 val = pair->error_domain + pair->error_code;
316 return g_int_hash (&val);
320 quark_code_pair_equal_func (const QuarkCodePair *a,
321 const QuarkCodePair *b)
323 return (a->error_domain == b->error_domain) && (a->error_code == b->error_code);
329 gchar *dbus_error_name;
333 registered_error_free (RegisteredError *re)
335 g_free (re->dbus_error_name);
339 G_LOCK_DEFINE_STATIC (error_lock);
341 /* maps from QuarkCodePair* -> RegisteredError* */
342 static GHashTable *quark_code_pair_to_re = NULL;
344 /* maps from gchar* -> RegisteredError* */
345 static GHashTable *dbus_error_name_to_re = NULL;
348 * g_dbus_error_register_error:
349 * @error_domain: A #GQuark for an error domain.
350 * @error_code: An error code.
351 * @dbus_error_name: A D-Bus error name.
353 * Creates an association to map between @dbus_error_name and
354 * #GErrors specified by @error_domain and @error_code.
356 * This is typically done in the routine that returns the #GQuark for
359 * Returns: %TRUE if the association was created, %FALSE if it already
365 g_dbus_error_register_error (GQuark error_domain,
367 const gchar *dbus_error_name)
373 g_return_val_if_fail (dbus_error_name != NULL, FALSE);
379 if (quark_code_pair_to_re == NULL)
381 g_assert (dbus_error_name_to_re == NULL); /* check invariant */
382 quark_code_pair_to_re = g_hash_table_new ((GHashFunc) quark_code_pair_hash_func,
383 (GEqualFunc) quark_code_pair_equal_func);
384 dbus_error_name_to_re = g_hash_table_new_full (g_str_hash,
387 (GDestroyNotify) registered_error_free);
390 if (g_hash_table_lookup (dbus_error_name_to_re, dbus_error_name) != NULL)
393 pair.error_domain = error_domain;
394 pair.error_code = error_code;
396 if (g_hash_table_lookup (quark_code_pair_to_re, &pair) != NULL)
399 re = g_new0 (RegisteredError, 1);
401 re->dbus_error_name = g_strdup (dbus_error_name);
403 g_hash_table_insert (quark_code_pair_to_re, &(re->pair), re);
404 g_hash_table_insert (dbus_error_name_to_re, re->dbus_error_name, re);
409 G_UNLOCK (error_lock);
414 * g_dbus_error_unregister_error:
415 * @error_domain: A #GQuark for an error domain.
416 * @error_code: An error code.
417 * @dbus_error_name: A D-Bus error name.
419 * Destroys an association previously set up with g_dbus_error_register_error().
421 * Returns: %TRUE if the association was destroyed, %FALSE if it wasn't found.
426 g_dbus_error_unregister_error (GQuark error_domain,
428 const gchar *dbus_error_name)
434 g_return_val_if_fail (dbus_error_name != NULL, FALSE);
440 if (dbus_error_name_to_re == NULL)
442 g_assert (quark_code_pair_to_re == NULL); /* check invariant */
446 re = g_hash_table_lookup (dbus_error_name_to_re, dbus_error_name);
450 pair.error_domain = error_domain;
451 pair.error_code = error_code;
452 g_warn_if_fail (g_hash_table_lookup (quark_code_pair_to_re, &pair) == NULL); /* check invariant */
458 g_warn_if_fail (g_hash_table_lookup (quark_code_pair_to_re, &(re->pair)) == re); /* check invariant */
460 g_warn_if_fail (g_hash_table_remove (quark_code_pair_to_re, &(re->pair)));
461 g_warn_if_fail (g_hash_table_remove (dbus_error_name_to_re, re->dbus_error_name));
463 /* destroy hashes if empty */
464 hash_size = g_hash_table_size (dbus_error_name_to_re);
467 g_warn_if_fail (g_hash_table_size (quark_code_pair_to_re) == 0); /* check invariant */
469 g_hash_table_unref (dbus_error_name_to_re);
470 dbus_error_name_to_re = NULL;
471 g_hash_table_unref (quark_code_pair_to_re);
472 quark_code_pair_to_re = NULL;
476 g_warn_if_fail (g_hash_table_size (quark_code_pair_to_re) == hash_size); /* check invariant */
480 G_UNLOCK (error_lock);
484 /* ---------------------------------------------------------------------------------------------------- */
487 * g_dbus_error_is_remote_error:
490 * Checks if @error represents an error received via D-Bus from a remote peer. If so,
491 * use g_dbus_error_get_remote_error() to get the name of the error.
493 * Returns: %TRUE if @error represents an error from a remote peer,
499 g_dbus_error_is_remote_error (const GError *error)
501 g_return_val_if_fail (error != NULL, FALSE);
502 return g_str_has_prefix (error->message, "GDBus.Error:");
507 * g_dbus_error_get_remote_error:
510 * Gets the D-Bus error name used for @error, if any.
512 * This function is guaranteed to return a D-Bus error name for all
513 * #GErrors returned from functions handling remote method calls
514 * (e.g. g_dbus_connection_call_finish()) unless
515 * g_dbus_error_strip_remote_error() has been used on @error.
517 * Returns: (nullable) (transfer full): an allocated string or %NULL if the
518 * D-Bus error name could not be found. Free with g_free().
523 g_dbus_error_get_remote_error (const GError *error)
528 g_return_val_if_fail (error != NULL, NULL);
530 /* Ensure that e.g. G_DBUS_ERROR is registered using g_dbus_error_register_error() */
531 _g_dbus_initialize ();
538 if (quark_code_pair_to_re != NULL)
541 pair.error_domain = error->domain;
542 pair.error_code = error->code;
543 g_assert (dbus_error_name_to_re != NULL); /* check invariant */
544 re = g_hash_table_lookup (quark_code_pair_to_re, &pair);
549 ret = g_strdup (re->dbus_error_name);
553 if (g_str_has_prefix (error->message, "GDBus.Error:"))
557 begin = error->message + sizeof ("GDBus.Error:") -1;
558 end = strstr (begin, ":");
559 if (end != NULL && end[1] == ' ')
561 ret = g_strndup (begin, end - begin);
566 G_UNLOCK (error_lock);
571 /* ---------------------------------------------------------------------------------------------------- */
574 * g_dbus_error_new_for_dbus_error:
575 * @dbus_error_name: D-Bus error name.
576 * @dbus_error_message: D-Bus error message.
578 * Creates a #GError based on the contents of @dbus_error_name and
579 * @dbus_error_message.
581 * Errors registered with g_dbus_error_register_error() will be looked
582 * up using @dbus_error_name and if a match is found, the error domain
583 * and code is used. Applications can use g_dbus_error_get_remote_error()
584 * to recover @dbus_error_name.
586 * If a match against a registered error is not found and the D-Bus
587 * error name is in a form as returned by g_dbus_error_encode_gerror()
588 * the error domain and code encoded in the name is used to
589 * create the #GError. Also, @dbus_error_name is added to the error message
590 * such that it can be recovered with g_dbus_error_get_remote_error().
592 * Otherwise, a #GError with the error code %G_IO_ERROR_DBUS_ERROR
593 * in the %G_IO_ERROR error domain is returned. Also, @dbus_error_name is
594 * added to the error message such that it can be recovered with
595 * g_dbus_error_get_remote_error().
597 * In all three cases, @dbus_error_name can always be recovered from the
598 * returned #GError using the g_dbus_error_get_remote_error() function
599 * (unless g_dbus_error_strip_remote_error() hasn't been used on the returned error).
601 * This function is typically only used in object mappings to prepare
602 * #GError instances for applications. Regular applications should not use
605 * Returns: (transfer full): An allocated #GError. Free with g_error_free().
610 g_dbus_error_new_for_dbus_error (const gchar *dbus_error_name,
611 const gchar *dbus_error_message)
616 g_return_val_if_fail (dbus_error_name != NULL, NULL);
617 g_return_val_if_fail (dbus_error_message != NULL, NULL);
619 /* Ensure that e.g. G_DBUS_ERROR is registered using g_dbus_error_register_error() */
620 _g_dbus_initialize ();
625 if (dbus_error_name_to_re != NULL)
627 g_assert (quark_code_pair_to_re != NULL); /* check invariant */
628 re = g_hash_table_lookup (dbus_error_name_to_re, dbus_error_name);
633 error = g_error_new (re->pair.error_domain,
635 "GDBus.Error:%s: %s",
641 GQuark error_domain = 0;
644 if (_g_dbus_error_decode_gerror (dbus_error_name,
648 error = g_error_new (error_domain,
650 "GDBus.Error:%s: %s",
656 error = g_error_new (G_IO_ERROR,
657 G_IO_ERROR_DBUS_ERROR,
658 "GDBus.Error:%s: %s",
664 G_UNLOCK (error_lock);
669 * g_dbus_error_set_dbus_error:
670 * @error: A pointer to a #GError or %NULL.
671 * @dbus_error_name: D-Bus error name.
672 * @dbus_error_message: D-Bus error message.
673 * @format: (nullable): printf()-style format to prepend to @dbus_error_message or %NULL.
674 * @...: Arguments for @format.
676 * Does nothing if @error is %NULL. Otherwise sets *@error to
677 * a new #GError created with g_dbus_error_new_for_dbus_error()
678 * with @dbus_error_message prepend with @format (unless %NULL).
683 g_dbus_error_set_dbus_error (GError **error,
684 const gchar *dbus_error_name,
685 const gchar *dbus_error_message,
689 g_return_if_fail (error == NULL || *error == NULL);
690 g_return_if_fail (dbus_error_name != NULL);
691 g_return_if_fail (dbus_error_message != NULL);
698 *error = g_dbus_error_new_for_dbus_error (dbus_error_name, dbus_error_message);
703 va_start (var_args, format);
704 g_dbus_error_set_dbus_error_valist (error,
714 * g_dbus_error_set_dbus_error_valist:
715 * @error: A pointer to a #GError or %NULL.
716 * @dbus_error_name: D-Bus error name.
717 * @dbus_error_message: D-Bus error message.
718 * @format: (nullable): printf()-style format to prepend to @dbus_error_message or %NULL.
719 * @var_args: Arguments for @format.
721 * Like g_dbus_error_set_dbus_error() but intended for language bindings.
726 g_dbus_error_set_dbus_error_valist (GError **error,
727 const gchar *dbus_error_name,
728 const gchar *dbus_error_message,
732 g_return_if_fail (error == NULL || *error == NULL);
733 g_return_if_fail (dbus_error_name != NULL);
734 g_return_if_fail (dbus_error_message != NULL);
743 message = g_strdup_vprintf (format, var_args);
744 s = g_strdup_printf ("%s: %s", message, dbus_error_message);
745 *error = g_dbus_error_new_for_dbus_error (dbus_error_name, s);
751 *error = g_dbus_error_new_for_dbus_error (dbus_error_name, dbus_error_message);
756 * g_dbus_error_strip_remote_error:
759 * Looks for extra information in the error message used to recover
760 * the D-Bus error name and strips it if found. If stripped, the
761 * message field in @error will correspond exactly to what was
762 * received on the wire.
764 * This is typically used when presenting errors to the end user.
766 * Returns: %TRUE if information was stripped, %FALSE otherwise.
771 g_dbus_error_strip_remote_error (GError *error)
775 g_return_val_if_fail (error != NULL, FALSE);
779 if (g_str_has_prefix (error->message, "GDBus.Error:"))
785 begin = error->message + sizeof ("GDBus.Error:") -1;
786 end = strstr (begin, ":");
787 if (end != NULL && end[1] == ' ')
789 new_message = g_strdup (end + 2);
790 g_free (error->message);
791 error->message = new_message;
800 * g_dbus_error_encode_gerror:
803 * Creates a D-Bus error name to use for @error. If @error matches
804 * a registered error (cf. g_dbus_error_register_error()), the corresponding
805 * D-Bus error name will be returned.
807 * Otherwise the a name of the form
808 * `org.gtk.GDBus.UnmappedGError.Quark._ESCAPED_QUARK_NAME.Code_ERROR_CODE`
809 * will be used. This allows other GDBus applications to map the error
810 * on the wire back to a #GError using g_dbus_error_new_for_dbus_error().
812 * This function is typically only used in object mappings to put a
813 * #GError on the wire. Regular applications should not use it.
815 * Returns: (transfer full) (not nullable): A D-Bus error name (never %NULL).
816 * Free with g_free().
821 g_dbus_error_encode_gerror (const GError *error)
826 g_return_val_if_fail (error != NULL, NULL);
828 /* Ensure that e.g. G_DBUS_ERROR is registered using g_dbus_error_register_error() */
829 _g_dbus_initialize ();
835 if (quark_code_pair_to_re != NULL)
838 pair.error_domain = error->domain;
839 pair.error_code = error->code;
840 g_assert (dbus_error_name_to_re != NULL); /* check invariant */
841 re = g_hash_table_lookup (quark_code_pair_to_re, &pair);
845 error_name = g_strdup (re->dbus_error_name);
846 G_UNLOCK (error_lock);
850 const gchar *domain_as_string;
854 G_UNLOCK (error_lock);
856 /* We can't make a lot of assumptions about what domain_as_string
857 * looks like and D-Bus is extremely picky about error names so
858 * hex-encode it for transport across the wire.
860 domain_as_string = g_quark_to_string (error->domain);
862 /* 0 is not a domain; neither are non-quark integers */
863 g_return_val_if_fail (domain_as_string != NULL, NULL);
865 s = g_string_new ("org.gtk.GDBus.UnmappedGError.Quark._");
866 for (n = 0; domain_as_string[n] != 0; n++)
868 gint c = domain_as_string[n];
869 if (g_ascii_isalnum (c))
871 g_string_append_c (s, c);
877 g_string_append_c (s, '_');
878 nibble_top = ((int) domain_as_string[n]) >> 4;
879 nibble_bottom = ((int) domain_as_string[n]) & 0x0f;
883 nibble_top += 'a' - 10;
884 if (nibble_bottom < 10)
885 nibble_bottom += '0';
887 nibble_bottom += 'a' - 10;
888 g_string_append_c (s, nibble_top);
889 g_string_append_c (s, nibble_bottom);
892 g_string_append_printf (s, ".Code%d", error->code);
893 error_name = g_string_free (s, FALSE);