(root)/
glib-2.79.0/
gio/
gdbuserror.c
       1  /* GDBus - GLib D-Bus Library
       2   *
       3   * Copyright (C) 2008-2010 Red Hat, Inc.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       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.
      11   *
      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.
      16   *
      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/>.
      19   *
      20   * Author: David Zeuthen <davidz@redhat.com>
      21   */
      22  
      23  #include "config.h"
      24  
      25  #include <stdlib.h>
      26  #include <string.h>
      27  
      28  #include "gdbuserror.h"
      29  #include "gioenums.h"
      30  #include "gioenumtypes.h"
      31  #include "gioerror.h"
      32  #include "gdbusprivate.h"
      33  
      34  #include "glibintl.h"
      35  
      36  static const GDBusErrorEntry g_dbus_error_entries[] =
      37  {
      38    {G_DBUS_ERROR_FAILED,                           "org.freedesktop.DBus.Error.Failed"},
      39    {G_DBUS_ERROR_NO_MEMORY,                        "org.freedesktop.DBus.Error.NoMemory"},
      40    {G_DBUS_ERROR_SERVICE_UNKNOWN,                  "org.freedesktop.DBus.Error.ServiceUnknown"},
      41    {G_DBUS_ERROR_NAME_HAS_NO_OWNER,                "org.freedesktop.DBus.Error.NameHasNoOwner"},
      42    {G_DBUS_ERROR_NO_REPLY,                         "org.freedesktop.DBus.Error.NoReply"},
      43    {G_DBUS_ERROR_IO_ERROR,                         "org.freedesktop.DBus.Error.IOError"},
      44    {G_DBUS_ERROR_BAD_ADDRESS,                      "org.freedesktop.DBus.Error.BadAddress"},
      45    {G_DBUS_ERROR_NOT_SUPPORTED,                    "org.freedesktop.DBus.Error.NotSupported"},
      46    {G_DBUS_ERROR_LIMITS_EXCEEDED,                  "org.freedesktop.DBus.Error.LimitsExceeded"},
      47    {G_DBUS_ERROR_ACCESS_DENIED,                    "org.freedesktop.DBus.Error.AccessDenied"},
      48    {G_DBUS_ERROR_AUTH_FAILED,                      "org.freedesktop.DBus.Error.AuthFailed"},
      49    {G_DBUS_ERROR_NO_SERVER,                        "org.freedesktop.DBus.Error.NoServer"},
      50    {G_DBUS_ERROR_TIMEOUT,                          "org.freedesktop.DBus.Error.Timeout"},
      51    {G_DBUS_ERROR_NO_NETWORK,                       "org.freedesktop.DBus.Error.NoNetwork"},
      52    {G_DBUS_ERROR_ADDRESS_IN_USE,                   "org.freedesktop.DBus.Error.AddressInUse"},
      53    {G_DBUS_ERROR_DISCONNECTED,                     "org.freedesktop.DBus.Error.Disconnected"},
      54    {G_DBUS_ERROR_INVALID_ARGS,                     "org.freedesktop.DBus.Error.InvalidArgs"},
      55    {G_DBUS_ERROR_FILE_NOT_FOUND,                   "org.freedesktop.DBus.Error.FileNotFound"},
      56    {G_DBUS_ERROR_FILE_EXISTS,                      "org.freedesktop.DBus.Error.FileExists"},
      57    {G_DBUS_ERROR_UNKNOWN_METHOD,                   "org.freedesktop.DBus.Error.UnknownMethod"},
      58    {G_DBUS_ERROR_TIMED_OUT,                        "org.freedesktop.DBus.Error.TimedOut"},
      59    {G_DBUS_ERROR_MATCH_RULE_NOT_FOUND,             "org.freedesktop.DBus.Error.MatchRuleNotFound"},
      60    {G_DBUS_ERROR_MATCH_RULE_INVALID,               "org.freedesktop.DBus.Error.MatchRuleInvalid"},
      61    {G_DBUS_ERROR_SPAWN_EXEC_FAILED,                "org.freedesktop.DBus.Error.Spawn.ExecFailed"},
      62    {G_DBUS_ERROR_SPAWN_FORK_FAILED,                "org.freedesktop.DBus.Error.Spawn.ForkFailed"},
      63    {G_DBUS_ERROR_SPAWN_CHILD_EXITED,               "org.freedesktop.DBus.Error.Spawn.ChildExited"},
      64    {G_DBUS_ERROR_SPAWN_CHILD_SIGNALED,             "org.freedesktop.DBus.Error.Spawn.ChildSignaled"},
      65    {G_DBUS_ERROR_SPAWN_FAILED,                     "org.freedesktop.DBus.Error.Spawn.Failed"},
      66    {G_DBUS_ERROR_SPAWN_SETUP_FAILED,               "org.freedesktop.DBus.Error.Spawn.FailedToSetup"},
      67    {G_DBUS_ERROR_SPAWN_CONFIG_INVALID,             "org.freedesktop.DBus.Error.Spawn.ConfigInvalid"},
      68    {G_DBUS_ERROR_SPAWN_SERVICE_INVALID,            "org.freedesktop.DBus.Error.Spawn.ServiceNotValid"},
      69    {G_DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,          "org.freedesktop.DBus.Error.Spawn.ServiceNotFound"},
      70    {G_DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,        "org.freedesktop.DBus.Error.Spawn.PermissionsInvalid"},
      71    {G_DBUS_ERROR_SPAWN_FILE_INVALID,               "org.freedesktop.DBus.Error.Spawn.FileInvalid"},
      72    {G_DBUS_ERROR_SPAWN_NO_MEMORY,                  "org.freedesktop.DBus.Error.Spawn.NoMemory"},
      73    {G_DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,          "org.freedesktop.DBus.Error.UnixProcessIdUnknown"},
      74    {G_DBUS_ERROR_INVALID_SIGNATURE,                "org.freedesktop.DBus.Error.InvalidSignature"},
      75    {G_DBUS_ERROR_INVALID_FILE_CONTENT,             "org.freedesktop.DBus.Error.InvalidFileContent"},
      76    {G_DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"},
      77    {G_DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,           "org.freedesktop.DBus.Error.AdtAuditDataUnknown"},
      78    {G_DBUS_ERROR_OBJECT_PATH_IN_USE,               "org.freedesktop.DBus.Error.ObjectPathInUse"},
      79    {G_DBUS_ERROR_UNKNOWN_OBJECT,                   "org.freedesktop.DBus.Error.UnknownObject"},
      80    {G_DBUS_ERROR_UNKNOWN_INTERFACE,                "org.freedesktop.DBus.Error.UnknownInterface"},
      81    {G_DBUS_ERROR_UNKNOWN_PROPERTY,                 "org.freedesktop.DBus.Error.UnknownProperty"},
      82    {G_DBUS_ERROR_PROPERTY_READ_ONLY,               "org.freedesktop.DBus.Error.PropertyReadOnly"},
      83  };
      84  
      85  GQuark
      86  g_dbus_error_quark (void)
      87  {
      88    G_STATIC_ASSERT (G_N_ELEMENTS (g_dbus_error_entries) - 1 == G_DBUS_ERROR_PROPERTY_READ_ONLY);
      89    static gsize quark = 0;
      90    g_dbus_error_register_error_domain ("g-dbus-error-quark",
      91                                        &quark,
      92                                        g_dbus_error_entries,
      93                                        G_N_ELEMENTS (g_dbus_error_entries));
      94    return (GQuark) quark;
      95  }
      96  
      97  /**
      98   * g_dbus_error_register_error_domain:
      99   * @error_domain_quark_name: The error domain name.
     100   * @quark_volatile: A pointer where to store the #GQuark.
     101   * @entries: (array length=num_entries): A pointer to @num_entries #GDBusErrorEntry struct items.
     102   * @num_entries: Number of items to register.
     103   *
     104   * Helper function for associating a #GError error domain with D-Bus error names.
     105   *
     106   * While @quark_volatile has a `volatile` qualifier, this is a historical
     107   * artifact and the argument passed to it should not be `volatile`.
     108   *
     109   * Since: 2.26
     110   */
     111  void
     112  g_dbus_error_register_error_domain (const gchar           *error_domain_quark_name,
     113                                      volatile gsize        *quark_volatile,
     114                                      const GDBusErrorEntry *entries,
     115                                      guint                  num_entries)
     116  {
     117    gsize *quark;
     118  
     119    g_return_if_fail (error_domain_quark_name != NULL);
     120    g_return_if_fail (quark_volatile != NULL);
     121    g_return_if_fail (entries != NULL);
     122    g_return_if_fail (num_entries > 0);
     123  
     124    /* Drop the volatile qualifier, which should never have been on the argument
     125     * in the first place. */
     126    quark = (gsize *) quark_volatile;
     127  
     128    if (g_once_init_enter (quark))
     129      {
     130        guint n;
     131        GQuark new_quark;
     132  
     133        new_quark = g_quark_from_static_string (error_domain_quark_name);
     134  
     135        for (n = 0; n < num_entries; n++)
     136          {
     137            g_warn_if_fail (g_dbus_error_register_error (new_quark,
     138                                                         entries[n].error_code,
     139                                                         entries[n].dbus_error_name));
     140          }
     141        g_once_init_leave (quark, new_quark);
     142      }
     143  }
     144  
     145  static gboolean
     146  _g_dbus_error_decode_gerror (const gchar *dbus_name,
     147                               GQuark      *out_error_domain,
     148                               gint        *out_error_code)
     149  {
     150    gboolean ret;
     151    guint n;
     152    GString *s;
     153    gchar *domain_quark_string;
     154  
     155    ret = FALSE;
     156    s = NULL;
     157  
     158    if (g_str_has_prefix (dbus_name, "org.gtk.GDBus.UnmappedGError.Quark._"))
     159      {
     160        s = g_string_new (NULL);
     161  
     162        for (n = sizeof "org.gtk.GDBus.UnmappedGError.Quark._" - 1;
     163             dbus_name[n] != '.' && dbus_name[n] != '\0';
     164             n++)
     165          {
     166            if (g_ascii_isalnum (dbus_name[n]))
     167              {
     168                g_string_append_c (s, dbus_name[n]);
     169              }
     170            else if (dbus_name[n] == '_')
     171              {
     172                guint nibble_top;
     173                guint nibble_bottom;
     174  
     175                n++;
     176  
     177                nibble_top = dbus_name[n];
     178                if (nibble_top >= '0' && nibble_top <= '9')
     179                  nibble_top -= '0';
     180                else if (nibble_top >= 'a' && nibble_top <= 'f')
     181                  nibble_top -= ('a' - 10);
     182                else
     183                  goto not_mapped;
     184  
     185                n++;
     186  
     187                nibble_bottom = dbus_name[n];
     188                if (nibble_bottom >= '0' && nibble_bottom <= '9')
     189                  nibble_bottom -= '0';
     190                else if (nibble_bottom >= 'a' && nibble_bottom <= 'f')
     191                  nibble_bottom -= ('a' - 10);
     192                else
     193                  goto not_mapped;
     194  
     195                g_string_append_c (s, (nibble_top<<4) | nibble_bottom);
     196              }
     197            else
     198              {
     199                goto not_mapped;
     200              }
     201          }
     202  
     203        if (!g_str_has_prefix (dbus_name + n, ".Code"))
     204          goto not_mapped;
     205  
     206        domain_quark_string = g_string_free (s, FALSE);
     207        s = NULL;
     208  
     209        if (out_error_domain != NULL)
     210          *out_error_domain = g_quark_from_string (domain_quark_string);
     211        g_free (domain_quark_string);
     212  
     213        if (out_error_code != NULL)
     214          *out_error_code = atoi (dbus_name + n + sizeof ".Code" - 1);
     215  
     216        ret = TRUE;
     217      }
     218  
     219   not_mapped:
     220  
     221    if (s != NULL)
     222      g_string_free (s, TRUE);
     223  
     224    return ret;
     225  }
     226  
     227  /* ---------------------------------------------------------------------------------------------------- */
     228  
     229  typedef struct
     230  {
     231    GQuark error_domain;
     232    gint   error_code;
     233  } QuarkCodePair;
     234  
     235  static guint
     236  quark_code_pair_hash_func (const QuarkCodePair *pair)
     237  {
     238    gint val;
     239    val = pair->error_domain + pair->error_code;
     240    return g_int_hash (&val);
     241  }
     242  
     243  static gboolean
     244  quark_code_pair_equal_func (const QuarkCodePair *a,
     245                              const QuarkCodePair *b)
     246  {
     247    return (a->error_domain == b->error_domain) && (a->error_code == b->error_code);
     248  }
     249  
     250  typedef struct
     251  {
     252    QuarkCodePair pair;
     253    gchar *dbus_error_name;
     254  } RegisteredError;
     255  
     256  static void
     257  registered_error_free (RegisteredError *re)
     258  {
     259    g_free (re->dbus_error_name);
     260    g_free (re);
     261  }
     262  
     263  G_LOCK_DEFINE_STATIC (error_lock);
     264  
     265  /* maps from QuarkCodePair* -> RegisteredError* */
     266  static GHashTable *quark_code_pair_to_re = NULL;
     267  
     268  /* maps from gchar* -> RegisteredError* */
     269  static GHashTable *dbus_error_name_to_re = NULL;
     270  
     271  /**
     272   * g_dbus_error_register_error:
     273   * @error_domain: A #GQuark for an error domain.
     274   * @error_code: An error code.
     275   * @dbus_error_name: A D-Bus error name.
     276   *
     277   * Creates an association to map between @dbus_error_name and
     278   * #GErrors specified by @error_domain and @error_code.
     279   *
     280   * This is typically done in the routine that returns the #GQuark for
     281   * an error domain.
     282   *
     283   * Returns: %TRUE if the association was created, %FALSE if it already
     284   * exists.
     285   *
     286   * Since: 2.26
     287   */
     288  gboolean
     289  g_dbus_error_register_error (GQuark       error_domain,
     290                               gint         error_code,
     291                               const gchar *dbus_error_name)
     292  {
     293    gboolean ret;
     294    QuarkCodePair pair;
     295    RegisteredError *re;
     296  
     297    g_return_val_if_fail (dbus_error_name != NULL, FALSE);
     298  
     299    ret = FALSE;
     300  
     301    G_LOCK (error_lock);
     302  
     303    if (quark_code_pair_to_re == NULL)
     304      {
     305        g_assert (dbus_error_name_to_re == NULL); /* check invariant */
     306        quark_code_pair_to_re = g_hash_table_new ((GHashFunc) quark_code_pair_hash_func,
     307                                                  (GEqualFunc) quark_code_pair_equal_func);
     308        dbus_error_name_to_re = g_hash_table_new_full (g_str_hash,
     309                                                       g_str_equal,
     310                                                       NULL,
     311                                                       (GDestroyNotify) registered_error_free);
     312      }
     313  
     314    if (g_hash_table_lookup (dbus_error_name_to_re, dbus_error_name) != NULL)
     315      goto out;
     316  
     317    pair.error_domain = error_domain;
     318    pair.error_code = error_code;
     319  
     320    if (g_hash_table_lookup (quark_code_pair_to_re, &pair) != NULL)
     321      goto out;
     322  
     323    re = g_new0 (RegisteredError, 1);
     324    re->pair = pair;
     325    re->dbus_error_name = g_strdup (dbus_error_name);
     326  
     327    g_hash_table_insert (quark_code_pair_to_re, &(re->pair), re);
     328    g_hash_table_insert (dbus_error_name_to_re, re->dbus_error_name, re);
     329  
     330    ret = TRUE;
     331  
     332   out:
     333    G_UNLOCK (error_lock);
     334    return ret;
     335  }
     336  
     337  /**
     338   * g_dbus_error_unregister_error:
     339   * @error_domain: A #GQuark for an error domain.
     340   * @error_code: An error code.
     341   * @dbus_error_name: A D-Bus error name.
     342   *
     343   * Destroys an association previously set up with g_dbus_error_register_error().
     344   *
     345   * Returns: %TRUE if the association was destroyed, %FALSE if it wasn't found.
     346   *
     347   * Since: 2.26
     348   */
     349  gboolean
     350  g_dbus_error_unregister_error (GQuark       error_domain,
     351                                 gint         error_code,
     352                                 const gchar *dbus_error_name)
     353  {
     354    gboolean ret;
     355    RegisteredError *re;
     356    guint hash_size;
     357  
     358    g_return_val_if_fail (dbus_error_name != NULL, FALSE);
     359  
     360    ret = FALSE;
     361  
     362    G_LOCK (error_lock);
     363  
     364    if (dbus_error_name_to_re == NULL)
     365      {
     366        g_assert (quark_code_pair_to_re == NULL); /* check invariant */
     367        goto out;
     368      }
     369  
     370    re = g_hash_table_lookup (dbus_error_name_to_re, dbus_error_name);
     371    if (re == NULL)
     372      {
     373        QuarkCodePair pair;
     374        pair.error_domain = error_domain;
     375        pair.error_code = error_code;
     376        g_warn_if_fail (g_hash_table_lookup (quark_code_pair_to_re, &pair) == NULL); /* check invariant */
     377        goto out;
     378      }
     379  
     380    ret = TRUE;
     381  
     382    g_warn_if_fail (g_hash_table_lookup (quark_code_pair_to_re, &(re->pair)) == re); /* check invariant */
     383  
     384    g_warn_if_fail (g_hash_table_remove (quark_code_pair_to_re, &(re->pair)));
     385    g_warn_if_fail (g_hash_table_remove (dbus_error_name_to_re, re->dbus_error_name));
     386  
     387    /* destroy hashes if empty */
     388    hash_size = g_hash_table_size (dbus_error_name_to_re);
     389    if (hash_size == 0)
     390      {
     391        g_warn_if_fail (g_hash_table_size (quark_code_pair_to_re) == 0); /* check invariant */
     392  
     393        g_hash_table_unref (dbus_error_name_to_re);
     394        dbus_error_name_to_re = NULL;
     395        g_hash_table_unref (quark_code_pair_to_re);
     396        quark_code_pair_to_re = NULL;
     397      }
     398    else
     399      {
     400        g_warn_if_fail (g_hash_table_size (quark_code_pair_to_re) == hash_size); /* check invariant */
     401      }
     402  
     403   out:
     404    G_UNLOCK (error_lock);
     405    return ret;
     406  }
     407  
     408  /* ---------------------------------------------------------------------------------------------------- */
     409  
     410  /**
     411   * g_dbus_error_is_remote_error:
     412   * @error: A #GError.
     413   *
     414   * Checks if @error represents an error received via D-Bus from a remote peer. If so,
     415   * use g_dbus_error_get_remote_error() to get the name of the error.
     416   *
     417   * Returns: %TRUE if @error represents an error from a remote peer,
     418   * %FALSE otherwise.
     419   *
     420   * Since: 2.26
     421   */
     422  gboolean
     423  g_dbus_error_is_remote_error (const GError *error)
     424  {
     425    g_return_val_if_fail (error != NULL, FALSE);
     426    return g_str_has_prefix (error->message, "GDBus.Error:");
     427  }
     428  
     429  
     430  /**
     431   * g_dbus_error_get_remote_error:
     432   * @error: a #GError
     433   *
     434   * Gets the D-Bus error name used for @error, if any.
     435   *
     436   * This function is guaranteed to return a D-Bus error name for all
     437   * #GErrors returned from functions handling remote method calls
     438   * (e.g. g_dbus_connection_call_finish()) unless
     439   * g_dbus_error_strip_remote_error() has been used on @error.
     440   *
     441   * Returns: (nullable) (transfer full): an allocated string or %NULL if the
     442   *     D-Bus error name could not be found. Free with g_free().
     443   *
     444   * Since: 2.26
     445   */
     446  gchar *
     447  g_dbus_error_get_remote_error (const GError *error)
     448  {
     449    RegisteredError *re;
     450    gchar *ret;
     451  
     452    g_return_val_if_fail (error != NULL, NULL);
     453  
     454    /* Ensure that e.g. G_DBUS_ERROR is registered using g_dbus_error_register_error() */
     455    _g_dbus_initialize ();
     456  
     457    ret = NULL;
     458  
     459    G_LOCK (error_lock);
     460  
     461    re = NULL;
     462    if (quark_code_pair_to_re != NULL)
     463      {
     464        QuarkCodePair pair;
     465        pair.error_domain = error->domain;
     466        pair.error_code = error->code;
     467        g_assert (dbus_error_name_to_re != NULL); /* check invariant */
     468        re = g_hash_table_lookup (quark_code_pair_to_re, &pair);
     469      }
     470  
     471    if (re != NULL)
     472      {
     473        ret = g_strdup (re->dbus_error_name);
     474      }
     475    else
     476      {
     477        if (g_str_has_prefix (error->message, "GDBus.Error:"))
     478          {
     479            const gchar *begin;
     480            const gchar *end;
     481            begin = error->message + sizeof ("GDBus.Error:") -1;
     482            end = strstr (begin, ":");
     483            if (end != NULL && end[1] == ' ')
     484              {
     485                ret = g_strndup (begin, end - begin);
     486              }
     487          }
     488      }
     489  
     490    G_UNLOCK (error_lock);
     491  
     492    return ret;
     493  }
     494  
     495  /* ---------------------------------------------------------------------------------------------------- */
     496  
     497  /**
     498   * g_dbus_error_new_for_dbus_error:
     499   * @dbus_error_name: D-Bus error name.
     500   * @dbus_error_message: D-Bus error message.
     501   *
     502   * Creates a #GError based on the contents of @dbus_error_name and
     503   * @dbus_error_message.
     504   *
     505   * Errors registered with g_dbus_error_register_error() will be looked
     506   * up using @dbus_error_name and if a match is found, the error domain
     507   * and code is used. Applications can use g_dbus_error_get_remote_error()
     508   * to recover @dbus_error_name.
     509   *
     510   * If a match against a registered error is not found and the D-Bus
     511   * error name is in a form as returned by g_dbus_error_encode_gerror()
     512   * the error domain and code encoded in the name is used to
     513   * create the #GError. Also, @dbus_error_name is added to the error message
     514   * such that it can be recovered with g_dbus_error_get_remote_error().
     515   *
     516   * Otherwise, a #GError with the error code %G_IO_ERROR_DBUS_ERROR
     517   * in the %G_IO_ERROR error domain is returned. Also, @dbus_error_name is
     518   * added to the error message such that it can be recovered with
     519   * g_dbus_error_get_remote_error().
     520   *
     521   * In all three cases, @dbus_error_name can always be recovered from the
     522   * returned #GError using the g_dbus_error_get_remote_error() function
     523   * (unless g_dbus_error_strip_remote_error() hasn't been used on the returned error).
     524   *
     525   * This function is typically only used in object mappings to prepare
     526   * #GError instances for applications. Regular applications should not use
     527   * it.
     528   *
     529   * Returns: (transfer full): An allocated #GError. Free with g_error_free().
     530   *
     531   * Since: 2.26
     532   */
     533  GError *
     534  g_dbus_error_new_for_dbus_error (const gchar *dbus_error_name,
     535                                   const gchar *dbus_error_message)
     536  {
     537    GError *error;
     538    RegisteredError *re;
     539  
     540    g_return_val_if_fail (dbus_error_name != NULL, NULL);
     541    g_return_val_if_fail (dbus_error_message != NULL, NULL);
     542  
     543    /* Ensure that e.g. G_DBUS_ERROR is registered using g_dbus_error_register_error() */
     544    _g_dbus_initialize ();
     545  
     546    G_LOCK (error_lock);
     547  
     548    re = NULL;
     549    if (dbus_error_name_to_re != NULL)
     550      {
     551        g_assert (quark_code_pair_to_re != NULL); /* check invariant */
     552        re = g_hash_table_lookup (dbus_error_name_to_re, dbus_error_name);
     553      }
     554  
     555    if (re != NULL)
     556      {
     557        error = g_error_new (re->pair.error_domain,
     558                             re->pair.error_code,
     559                             "GDBus.Error:%s: %s",
     560                             dbus_error_name,
     561                             dbus_error_message);
     562      }
     563    else
     564      {
     565        GQuark error_domain = 0;
     566        gint error_code = 0;
     567  
     568        if (_g_dbus_error_decode_gerror (dbus_error_name,
     569                                         &error_domain,
     570                                         &error_code))
     571          {
     572            error = g_error_new (error_domain,
     573                                 error_code,
     574                                 "GDBus.Error:%s: %s",
     575                                 dbus_error_name,
     576                                 dbus_error_message);
     577          }
     578        else
     579          {
     580            error = g_error_new (G_IO_ERROR,
     581                                 G_IO_ERROR_DBUS_ERROR,
     582                                 "GDBus.Error:%s: %s",
     583                                 dbus_error_name,
     584                                 dbus_error_message);
     585          }
     586      }
     587  
     588    G_UNLOCK (error_lock);
     589    return error;
     590  }
     591  
     592  /**
     593   * g_dbus_error_set_dbus_error:
     594   * @error: A pointer to a #GError or %NULL.
     595   * @dbus_error_name: D-Bus error name.
     596   * @dbus_error_message: D-Bus error message.
     597   * @format: (nullable): printf()-style format to prepend to @dbus_error_message or %NULL.
     598   * @...: Arguments for @format.
     599   *
     600   * Does nothing if @error is %NULL. Otherwise sets *@error to
     601   * a new #GError created with g_dbus_error_new_for_dbus_error()
     602   * with @dbus_error_message prepend with @format (unless %NULL).
     603   *
     604   * Since: 2.26
     605   */
     606  void
     607  g_dbus_error_set_dbus_error (GError      **error,
     608                               const gchar  *dbus_error_name,
     609                               const gchar  *dbus_error_message,
     610                               const gchar  *format,
     611                               ...)
     612  {
     613    g_return_if_fail (error == NULL || *error == NULL);
     614    g_return_if_fail (dbus_error_name != NULL);
     615    g_return_if_fail (dbus_error_message != NULL);
     616  
     617    if (error == NULL)
     618      return;
     619  
     620    if (format == NULL)
     621      {
     622        *error = g_dbus_error_new_for_dbus_error (dbus_error_name, dbus_error_message);
     623      }
     624    else
     625      {
     626        va_list var_args;
     627        va_start (var_args, format);
     628        g_dbus_error_set_dbus_error_valist (error,
     629                                            dbus_error_name,
     630                                            dbus_error_message,
     631                                            format,
     632                                            var_args);
     633        va_end (var_args);
     634      }
     635  }
     636  
     637  /**
     638   * g_dbus_error_set_dbus_error_valist:
     639   * @error: A pointer to a #GError or %NULL.
     640   * @dbus_error_name: D-Bus error name.
     641   * @dbus_error_message: D-Bus error message.
     642   * @format: (nullable): printf()-style format to prepend to @dbus_error_message or %NULL.
     643   * @var_args: Arguments for @format.
     644   *
     645   * Like g_dbus_error_set_dbus_error() but intended for language bindings.
     646   *
     647   * Since: 2.26
     648   */
     649  void
     650  g_dbus_error_set_dbus_error_valist (GError      **error,
     651                                      const gchar  *dbus_error_name,
     652                                      const gchar  *dbus_error_message,
     653                                      const gchar  *format,
     654                                      va_list       var_args)
     655  {
     656    g_return_if_fail (error == NULL || *error == NULL);
     657    g_return_if_fail (dbus_error_name != NULL);
     658    g_return_if_fail (dbus_error_message != NULL);
     659  
     660    if (error == NULL)
     661      return;
     662  
     663    if (format != NULL)
     664      {
     665        gchar *message;
     666        gchar *s;
     667        message = g_strdup_vprintf (format, var_args);
     668        s = g_strdup_printf ("%s: %s", message, dbus_error_message);
     669        *error = g_dbus_error_new_for_dbus_error (dbus_error_name, s);
     670        g_free (s);
     671        g_free (message);
     672      }
     673    else
     674      {
     675        *error = g_dbus_error_new_for_dbus_error (dbus_error_name, dbus_error_message);
     676      }
     677  }
     678  
     679  /**
     680   * g_dbus_error_strip_remote_error:
     681   * @error: A #GError.
     682   *
     683   * Looks for extra information in the error message used to recover
     684   * the D-Bus error name and strips it if found. If stripped, the
     685   * message field in @error will correspond exactly to what was
     686   * received on the wire.
     687   *
     688   * This is typically used when presenting errors to the end user.
     689   *
     690   * Returns: %TRUE if information was stripped, %FALSE otherwise.
     691   *
     692   * Since: 2.26
     693   */
     694  gboolean
     695  g_dbus_error_strip_remote_error (GError *error)
     696  {
     697    gboolean ret;
     698  
     699    g_return_val_if_fail (error != NULL, FALSE);
     700  
     701    ret = FALSE;
     702  
     703    if (g_str_has_prefix (error->message, "GDBus.Error:"))
     704      {
     705        const gchar *begin;
     706        const gchar *end;
     707        gchar *new_message;
     708  
     709        begin = error->message + sizeof ("GDBus.Error:") -1;
     710        end = strstr (begin, ":");
     711        if (end != NULL && end[1] == ' ')
     712          {
     713            new_message = g_strdup (end + 2);
     714            g_free (error->message);
     715            error->message = new_message;
     716            ret = TRUE;
     717          }
     718      }
     719  
     720    return ret;
     721  }
     722  
     723  /**
     724   * g_dbus_error_encode_gerror:
     725   * @error: A #GError.
     726   *
     727   * Creates a D-Bus error name to use for @error. If @error matches
     728   * a registered error (cf. g_dbus_error_register_error()), the corresponding
     729   * D-Bus error name will be returned.
     730   *
     731   * Otherwise the a name of the form
     732   * `org.gtk.GDBus.UnmappedGError.Quark._ESCAPED_QUARK_NAME.Code_ERROR_CODE`
     733   * will be used. This allows other GDBus applications to map the error
     734   * on the wire back to a #GError using g_dbus_error_new_for_dbus_error().
     735   *
     736   * This function is typically only used in object mappings to put a
     737   * #GError on the wire. Regular applications should not use it.
     738   *
     739   * Returns: (transfer full) (not nullable): A D-Bus error name (never %NULL).
     740   *     Free with g_free().
     741   *
     742   * Since: 2.26
     743   */
     744  gchar *
     745  g_dbus_error_encode_gerror (const GError *error)
     746  {
     747    RegisteredError *re;
     748    gchar *error_name;
     749  
     750    g_return_val_if_fail (error != NULL, NULL);
     751  
     752    /* Ensure that e.g. G_DBUS_ERROR is registered using g_dbus_error_register_error() */
     753    _g_dbus_initialize ();
     754  
     755    error_name = NULL;
     756  
     757    G_LOCK (error_lock);
     758    re = NULL;
     759    if (quark_code_pair_to_re != NULL)
     760      {
     761        QuarkCodePair pair;
     762        pair.error_domain = error->domain;
     763        pair.error_code = error->code;
     764        g_assert (dbus_error_name_to_re != NULL); /* check invariant */
     765        re = g_hash_table_lookup (quark_code_pair_to_re, &pair);
     766      }
     767    if (re != NULL)
     768      {
     769        error_name = g_strdup (re->dbus_error_name);
     770        G_UNLOCK (error_lock);
     771      }
     772    else
     773      {
     774        const gchar *domain_as_string;
     775        GString *s;
     776        guint n;
     777  
     778        G_UNLOCK (error_lock);
     779  
     780        /* We can't make a lot of assumptions about what domain_as_string
     781         * looks like and D-Bus is extremely picky about error names so
     782         * hex-encode it for transport across the wire.
     783         */
     784        domain_as_string = g_quark_to_string (error->domain);
     785  
     786        /* 0 is not a domain; neither are non-quark integers */
     787        g_return_val_if_fail (domain_as_string != NULL, NULL);
     788  
     789        s = g_string_new ("org.gtk.GDBus.UnmappedGError.Quark._");
     790        for (n = 0; domain_as_string[n] != 0; n++)
     791          {
     792            gint c = domain_as_string[n];
     793            if (g_ascii_isalnum (c))
     794              {
     795                g_string_append_c (s, c);
     796              }
     797            else
     798              {
     799                guint nibble_top;
     800                guint nibble_bottom;
     801                g_string_append_c (s, '_');
     802                nibble_top = ((int) domain_as_string[n]) >> 4;
     803                nibble_bottom = ((int) domain_as_string[n]) & 0x0f;
     804                if (nibble_top < 10)
     805                  nibble_top += '0';
     806                else
     807                  nibble_top += 'a' - 10;
     808                if (nibble_bottom < 10)
     809                  nibble_bottom += '0';
     810                else
     811                  nibble_bottom += 'a' - 10;
     812                g_string_append_c (s, nibble_top);
     813                g_string_append_c (s, nibble_bottom);
     814              }
     815          }
     816        g_string_append_printf (s, ".Code%d", error->code);
     817        error_name = g_string_free (s, FALSE);
     818      }
     819  
     820    return error_name;
     821  }