Blame glib/gerror.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:error_reporting
Packit ae235b
 * @Title: Error Reporting
Packit ae235b
 * @Short_description: a system for reporting errors
Packit ae235b
 *
Packit ae235b
 * GLib provides a standard method of reporting errors from a called
Packit ae235b
 * function to the calling code. (This is the same problem solved by
Packit ae235b
 * exceptions in other languages.) It's important to understand that
Packit ae235b
 * this method is both a data type (the #GError struct) and a [set of
Packit ae235b
 * rules][gerror-rules]. If you use #GError incorrectly, then your code will not
Packit ae235b
 * properly interoperate with other code that uses #GError, and users
Packit ae235b
 * of your API will probably get confused. In most cases, [using #GError is
Packit ae235b
 * preferred over numeric error codes][gerror-comparison], but there are
Packit ae235b
 * situations where numeric error codes are useful for performance.
Packit ae235b
 *
Packit ae235b
 * First and foremost: #GError should only be used to report recoverable
Packit ae235b
 * runtime errors, never to report programming errors. If the programmer
Packit ae235b
 * has screwed up, then you should use g_warning(), g_return_if_fail(),
Packit ae235b
 * g_assert(), g_error(), or some similar facility. (Incidentally,
Packit ae235b
 * remember that the g_error() function should only be used for
Packit ae235b
 * programming errors, it should not be used to print any error
Packit ae235b
 * reportable via #GError.)
Packit ae235b
 *
Packit ae235b
 * Examples of recoverable runtime errors are "file not found" or
Packit ae235b
 * "failed to parse input." Examples of programming errors are "NULL
Packit ae235b
 * passed to strcmp()" or "attempted to free the same pointer twice."
Packit ae235b
 * These two kinds of errors are fundamentally different: runtime errors
Packit ae235b
 * should be handled or reported to the user, programming errors should
Packit ae235b
 * be eliminated by fixing the bug in the program. This is why most
Packit ae235b
 * functions in GLib and GTK+ do not use the #GError facility.
Packit ae235b
 *
Packit ae235b
 * Functions that can fail take a return location for a #GError as their
Packit ae235b
 * last argument. On error, a new #GError instance will be allocated and
Packit ae235b
 * returned to the caller via this argument. For example:
Packit ae235b
 * |[
Packit ae235b
 * gboolean g_file_get_contents (const gchar  *filename,
Packit ae235b
 *                               gchar       **contents,
Packit ae235b
 *                               gsize        *length,
Packit ae235b
 *                               GError      **error);
Packit ae235b
 * ]|
Packit ae235b
 * If you pass a non-%NULL value for the `error` argument, it should
Packit ae235b
 * point to a location where an error can be placed. For example:
Packit ae235b
 * |[
Packit ae235b
 * gchar *contents;
Packit ae235b
 * GError *err = NULL;
Packit ae235b
 *
Packit ae235b
 * g_file_get_contents ("foo.txt", &contents, NULL, &err;;
Packit ae235b
 * g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL));
Packit ae235b
 * if (err != NULL)
Packit ae235b
 *   {
Packit ae235b
 *     // Report error to user, and free error
Packit ae235b
 *     g_assert (contents == NULL);
Packit ae235b
 *     fprintf (stderr, "Unable to read file: %s\n", err->message);
Packit ae235b
 *     g_error_free (err);
Packit ae235b
 *   }
Packit ae235b
 * else
Packit ae235b
 *   {
Packit ae235b
 *     // Use file contents
Packit ae235b
 *     g_assert (contents != NULL);
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 * Note that `err != NULL` in this example is a reliable indicator
Packit ae235b
 * of whether g_file_get_contents() failed. Additionally,
Packit ae235b
 * g_file_get_contents() returns a boolean which
Packit ae235b
 * indicates whether it was successful.
Packit ae235b
 *
Packit ae235b
 * Because g_file_get_contents() returns %FALSE on failure, if you
Packit ae235b
 * are only interested in whether it failed and don't need to display
Packit ae235b
 * an error message, you can pass %NULL for the @error argument:
Packit ae235b
 * |[
Packit ae235b
 * if (g_file_get_contents ("foo.txt", &contents, NULL, NULL)) // ignore errors
Packit ae235b
 *   // no error occurred 
Packit ae235b
 *   ;
Packit ae235b
 * else
Packit ae235b
 *   // error
Packit ae235b
 *   ;
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * The #GError object contains three fields: @domain indicates the module
Packit ae235b
 * the error-reporting function is located in, @code indicates the specific
Packit ae235b
 * error that occurred, and @message is a user-readable error message with
Packit ae235b
 * as many details as possible. Several functions are provided to deal
Packit ae235b
 * with an error received from a called function: g_error_matches()
Packit ae235b
 * returns %TRUE if the error matches a given domain and code,
Packit ae235b
 * g_propagate_error() copies an error into an error location (so the
Packit ae235b
 * calling function will receive it), and g_clear_error() clears an
Packit ae235b
 * error location by freeing the error and resetting the location to
Packit ae235b
 * %NULL. To display an error to the user, simply display the @message,
Packit ae235b
 * perhaps along with additional context known only to the calling
Packit ae235b
 * function (the file being opened, or whatever - though in the
Packit ae235b
 * g_file_get_contents() case, the @message already contains a filename).
Packit ae235b
 *
Packit ae235b
 * When implementing a function that can report errors, the basic
Packit ae235b
 * tool is g_set_error(). Typically, if a fatal error occurs you
Packit ae235b
 * want to g_set_error(), then return immediately. g_set_error()
Packit ae235b
 * does nothing if the error location passed to it is %NULL.
Packit ae235b
 * Here's an example:
Packit ae235b
 * |[
Packit ae235b
 * gint
Packit ae235b
 * foo_open_file (GError **error)
Packit ae235b
 * {
Packit ae235b
 *   gint fd;
Packit ae235b
 *   int saved_errno;
Packit ae235b
 *
Packit ae235b
 *   fd = open ("file.txt", O_RDONLY);
Packit ae235b
 *   saved_errno = errno;
Packit ae235b
 *
Packit ae235b
 *   if (fd < 0)
Packit ae235b
 *     {
Packit ae235b
 *       g_set_error (error,
Packit ae235b
 *                    FOO_ERROR,                 // error domain
Packit ae235b
 *                    FOO_ERROR_BLAH,            // error code
Packit ae235b
 *                    "Failed to open file: %s", // error message format string
Packit ae235b
 *                    g_strerror (saved_errno));
Packit ae235b
 *       return -1;
Packit ae235b
 *     }
Packit ae235b
 *   else
Packit ae235b
 *     return fd;
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Things are somewhat more complicated if you yourself call another
Packit ae235b
 * function that can report a #GError. If the sub-function indicates
Packit ae235b
 * fatal errors in some way other than reporting a #GError, such as
Packit ae235b
 * by returning %TRUE on success, you can simply do the following:
Packit ae235b
 * |[
Packit ae235b
 * gboolean
Packit ae235b
 * my_function_that_can_fail (GError **err)
Packit ae235b
 * {
Packit ae235b
 *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
Packit ae235b
 *
Packit ae235b
 *   if (!sub_function_that_can_fail (err))
Packit ae235b
 *     {
Packit ae235b
 *       // assert that error was set by the sub-function
Packit ae235b
 *       g_assert (err == NULL || *err != NULL);
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   // otherwise continue, no error occurred
Packit ae235b
 *   g_assert (err == NULL || *err == NULL);
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * If the sub-function does not indicate errors other than by
Packit ae235b
 * reporting a #GError (or if its return value does not reliably indicate
Packit ae235b
 * errors) you need to create a temporary #GError
Packit ae235b
 * since the passed-in one may be %NULL. g_propagate_error() is
Packit ae235b
 * intended for use in this case.
Packit ae235b
 * |[
Packit ae235b
 * gboolean
Packit ae235b
 * my_function_that_can_fail (GError **err)
Packit ae235b
 * {
Packit ae235b
 *   GError *tmp_error;
Packit ae235b
 *
Packit ae235b
 *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
Packit ae235b
 *
Packit ae235b
 *   tmp_error = NULL;
Packit ae235b
 *   sub_function_that_can_fail (&tmp_error);
Packit ae235b
 *
Packit ae235b
 *   if (tmp_error != NULL)
Packit ae235b
 *     {
Packit ae235b
 *       // store tmp_error in err, if err != NULL,
Packit ae235b
 *       // otherwise call g_error_free() on tmp_error
Packit ae235b
 *       g_propagate_error (err, tmp_error);
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   // otherwise continue, no error occurred
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Error pileups are always a bug. For example, this code is incorrect:
Packit ae235b
 * |[
Packit ae235b
 * gboolean
Packit ae235b
 * my_function_that_can_fail (GError **err)
Packit ae235b
 * {
Packit ae235b
 *   GError *tmp_error;
Packit ae235b
 *
Packit ae235b
 *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
Packit ae235b
 *
Packit ae235b
 *   tmp_error = NULL;
Packit ae235b
 *   sub_function_that_can_fail (&tmp_error);
Packit ae235b
 *   other_function_that_can_fail (&tmp_error);
Packit ae235b
 *
Packit ae235b
 *   if (tmp_error != NULL)
Packit ae235b
 *     {
Packit ae235b
 *       g_propagate_error (err, tmp_error);
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 * @tmp_error should be checked immediately after sub_function_that_can_fail(),
Packit ae235b
 * and either cleared or propagated upward. The rule is: after each error,
Packit ae235b
 * you must either handle the error, or return it to the calling function.
Packit ae235b
 *
Packit ae235b
 * Note that passing %NULL for the error location is the equivalent
Packit ae235b
 * of handling an error by always doing nothing about it. So the
Packit ae235b
 * following code is fine, assuming errors in sub_function_that_can_fail()
Packit ae235b
 * are not fatal to my_function_that_can_fail():
Packit ae235b
 * |[
Packit ae235b
 * gboolean
Packit ae235b
 * my_function_that_can_fail (GError **err)
Packit ae235b
 * {
Packit ae235b
 *   GError *tmp_error;
Packit ae235b
 *
Packit ae235b
 *   g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
Packit ae235b
 *
Packit ae235b
 *   sub_function_that_can_fail (NULL); // ignore errors
Packit ae235b
 *
Packit ae235b
 *   tmp_error = NULL;
Packit ae235b
 *   other_function_that_can_fail (&tmp_error);
Packit ae235b
 *
Packit ae235b
 *   if (tmp_error != NULL)
Packit ae235b
 *     {
Packit ae235b
 *       g_propagate_error (err, tmp_error);
Packit ae235b
 *       return FALSE;
Packit ae235b
 *     }
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Note that passing %NULL for the error location ignores errors;
Packit ae235b
 * it's equivalent to
Packit ae235b
 * `try { sub_function_that_can_fail (); } catch (...) {}`
Packit ae235b
 * in C++. It does not mean to leave errors unhandled; it means
Packit ae235b
 * to handle them by doing nothing.
Packit ae235b
 *
Packit ae235b
 * Error domains and codes are conventionally named as follows:
Packit ae235b
 *
Packit ae235b
 * - The error domain is called <NAMESPACE>_<MODULE>_ERROR,
Packit ae235b
 *   for example %G_SPAWN_ERROR or %G_THREAD_ERROR:
Packit ae235b
 *   |[
Packit ae235b
 *   #define G_SPAWN_ERROR g_spawn_error_quark ()
Packit ae235b
 *
Packit ae235b
 *   GQuark
Packit ae235b
 *   g_spawn_error_quark (void)
Packit ae235b
 *   {
Packit ae235b
 *       return g_quark_from_static_string ("g-spawn-error-quark");
Packit ae235b
 *   }
Packit ae235b
 *   ]|
Packit ae235b
 *
Packit ae235b
 * - The quark function for the error domain is called
Packit ae235b
 *   <namespace>_<module>_error_quark,
Packit ae235b
 *   for example g_spawn_error_quark() or g_thread_error_quark().
Packit ae235b
 *
Packit ae235b
 * - The error codes are in an enumeration called
Packit ae235b
 *   <Namespace><Module>Error;
Packit ae235b
 *   for example, #GThreadError or #GSpawnError.
Packit ae235b
 *
Packit ae235b
 * - Members of the error code enumeration are called
Packit ae235b
 *   <NAMESPACE>_<MODULE>_ERROR_,
Packit ae235b
 *   for example %G_SPAWN_ERROR_FORK or %G_THREAD_ERROR_AGAIN.
Packit ae235b
 *
Packit ae235b
 * - If there's a "generic" or "unknown" error code for unrecoverable
Packit ae235b
 *   errors it doesn't make sense to distinguish with specific codes,
Packit ae235b
 *   it should be called <NAMESPACE>_<MODULE>_ERROR_FAILED,
Packit ae235b
 *   for example %G_SPAWN_ERROR_FAILED. In the case of error code
Packit ae235b
 *   enumerations that may be extended in future releases, you should
Packit ae235b
 *   generally not handle this error code explicitly, but should
Packit ae235b
 *   instead treat any unrecognized error code as equivalent to
Packit ae235b
 *   FAILED.
Packit ae235b
 *
Packit ae235b
 * ## Comparison of #GError and traditional error handling # {#gerror-comparison}
Packit ae235b
 *
Packit ae235b
 * #GError has several advantages over traditional numeric error codes:
Packit ae235b
 * importantly, tools like
Packit ae235b
 * [gobject-introspection](https://developer.gnome.org/gi/stable/) understand
Packit ae235b
 * #GErrors and convert them to exceptions in bindings; the message includes
Packit ae235b
 * more information than just a code; and use of a domain helps prevent
Packit ae235b
 * misinterpretation of error codes.
Packit ae235b
 *
Packit ae235b
 * #GError has disadvantages though: it requires a memory allocation, and
Packit ae235b
 * formatting the error message string has a performance overhead. This makes it
Packit ae235b
 * unsuitable for use in retry loops where errors are a common case, rather than
Packit ae235b
 * being unusual. For example, using %G_IO_ERROR_WOULD_BLOCK means hitting these
Packit ae235b
 * overheads in the normal control flow. String formatting overhead can be
Packit ae235b
 * eliminated by using g_set_error_literal() in some cases.
Packit ae235b
 *
Packit ae235b
 * These performance issues can be compounded if a function wraps the #GErrors
Packit ae235b
 * returned by the functions it calls: this multiplies the number of allocations
Packit ae235b
 * and string formatting operations. This can be partially mitigated by using
Packit ae235b
 * g_prefix_error().
Packit ae235b
 *
Packit ae235b
 * ## Rules for use of #GError # {#gerror-rules}
Packit ae235b
 *
Packit ae235b
 * Summary of rules for use of #GError:
Packit ae235b
 *
Packit ae235b
 * - Do not report programming errors via #GError.
Packit ae235b
 * 
Packit ae235b
 * - The last argument of a function that returns an error should
Packit ae235b
 *   be a location where a #GError can be placed (i.e. "#GError** error").
Packit ae235b
 *   If #GError is used with varargs, the #GError** should be the last
Packit ae235b
 *   argument before the "...".
Packit ae235b
 *
Packit ae235b
 * - The caller may pass %NULL for the #GError** if they are not interested
Packit ae235b
 *   in details of the exact error that occurred.
Packit ae235b
 *
Packit ae235b
 * - If %NULL is passed for the #GError** argument, then errors should
Packit ae235b
 *   not be returned to the caller, but your function should still
Packit ae235b
 *   abort and return if an error occurs. That is, control flow should
Packit ae235b
 *   not be affected by whether the caller wants to get a #GError.
Packit ae235b
 *
Packit ae235b
 * - If a #GError is reported, then your function by definition had a
Packit ae235b
 *   fatal failure and did not complete whatever it was supposed to do.
Packit ae235b
 *   If the failure was not fatal, then you handled it and you should not
Packit ae235b
 *   report it. If it was fatal, then you must report it and discontinue
Packit ae235b
 *   whatever you were doing immediately.
Packit ae235b
 *
Packit ae235b
 * - If a #GError is reported, out parameters are not guaranteed to
Packit ae235b
 *   be set to any defined value.
Packit ae235b
 *
Packit ae235b
 * - A #GError* must be initialized to %NULL before passing its address
Packit ae235b
 *   to a function that can report errors.
Packit ae235b
 *
Packit ae235b
 * - "Piling up" errors is always a bug. That is, if you assign a
Packit ae235b
 *   new #GError to a #GError* that is non-%NULL, thus overwriting
Packit ae235b
 *   the previous error, it indicates that you should have aborted
Packit ae235b
 *   the operation instead of continuing. If you were able to continue,
Packit ae235b
 *   you should have cleared the previous error with g_clear_error().
Packit ae235b
 *   g_set_error() will complain if you pile up errors.
Packit ae235b
 *
Packit ae235b
 * - By convention, if you return a boolean value indicating success
Packit ae235b
 *   then %TRUE means success and %FALSE means failure. Avoid creating
Packit ae235b
 *   functions which have a boolean return value and a GError parameter,
Packit ae235b
 *   but where the boolean does something other than signal whether the
Packit ae235b
 *   GError is set.  Among other problems, it requires C callers to allocate
Packit ae235b
 *   a temporary error.  Instead, provide a "gboolean *" out parameter.
Packit ae235b
 *   There are functions in GLib itself such as g_key_file_has_key() that
Packit ae235b
 *   are deprecated because of this. If %FALSE is returned, the error must
Packit ae235b
 *   be set to a non-%NULL value.  One exception to this is that in situations
Packit ae235b
 *   that are already considered to be undefined behaviour (such as when a
Packit ae235b
 *   g_return_val_if_fail() check fails), the error need not be set.
Packit ae235b
 *   Instead of checking separately whether the error is set, callers
Packit ae235b
 *   should ensure that they do not provoke undefined behaviour, then
Packit ae235b
 *   assume that the error will be set on failure.
Packit ae235b
 *
Packit ae235b
 * - A %NULL return value is also frequently used to mean that an error
Packit ae235b
 *   occurred. You should make clear in your documentation whether %NULL
Packit ae235b
 *   is a valid return value in non-error cases; if %NULL is a valid value,
Packit ae235b
 *   then users must check whether an error was returned to see if the
Packit ae235b
 *   function succeeded.
Packit ae235b
 *
Packit ae235b
 * - When implementing a function that can report errors, you may want
Packit ae235b
 *   to add a check at the top of your function that the error return
Packit ae235b
 *   location is either %NULL or contains a %NULL error (e.g.
Packit ae235b
 *   `g_return_if_fail (error == NULL || *error == NULL);`).
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gerror.h"
Packit ae235b
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error_new_valist:
Packit ae235b
 * @domain: error domain
Packit ae235b
 * @code: error code
Packit ae235b
 * @format: printf()-style format for error message
Packit ae235b
 * @args: #va_list of parameters for the message format
Packit ae235b
 *
Packit ae235b
 * Creates a new #GError with the given @domain and @code,
Packit ae235b
 * and a message formatted with @format.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GError
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
GError*
Packit ae235b
g_error_new_valist (GQuark       domain,
Packit ae235b
                    gint         code,
Packit ae235b
                    const gchar *format,
Packit ae235b
                    va_list      args)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  /* Historically, GError allowed this (although it was never meant to work),
Packit ae235b
   * and it has significant use in the wild, which g_return_val_if_fail
Packit ae235b
   * would break. It should maybe g_return_val_if_fail in GLib 4.
Packit ae235b
   * (GNOME#660371, GNOME#560482)
Packit ae235b
   */
Packit ae235b
  g_warn_if_fail (domain != 0);
Packit ae235b
  g_warn_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  error = g_slice_new (GError);
Packit ae235b
Packit ae235b
  error->domain = domain;
Packit ae235b
  error->code = code;
Packit ae235b
  error->message = g_strdup_vprintf (format, args);
Packit ae235b
Packit ae235b
  return error;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error_new:
Packit ae235b
 * @domain: error domain
Packit ae235b
 * @code: error code
Packit ae235b
 * @format: printf()-style format for error message
Packit ae235b
 * @...: parameters for message format
Packit ae235b
 *
Packit ae235b
 * Creates a new #GError with the given @domain and @code,
Packit ae235b
 * and a message formatted with @format.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GError
Packit ae235b
 */
Packit ae235b
GError*
Packit ae235b
g_error_new (GQuark       domain,
Packit ae235b
             gint         code,
Packit ae235b
             const gchar *format,
Packit ae235b
             ...)
Packit ae235b
{
Packit ae235b
  GError* error;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (format != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (domain != 0, NULL);
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  error = g_error_new_valist (domain, code, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  return error;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error_new_literal:
Packit ae235b
 * @domain: error domain
Packit ae235b
 * @code: error code
Packit ae235b
 * @message: error message
Packit ae235b
 *
Packit ae235b
 * Creates a new #GError; unlike g_error_new(), @message is
Packit ae235b
 * not a printf()-style format string. Use this function if
Packit ae235b
 * @message contains text you don't have control over,
Packit ae235b
 * that could include printf() escape sequences.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GError
Packit ae235b
 **/
Packit ae235b
GError*
Packit ae235b
g_error_new_literal (GQuark         domain,
Packit ae235b
                     gint           code,
Packit ae235b
                     const gchar   *message)
Packit ae235b
{
Packit ae235b
  GError* err;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (message != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (domain != 0, NULL);
Packit ae235b
Packit ae235b
  err = g_slice_new (GError);
Packit ae235b
Packit ae235b
  err->domain = domain;
Packit ae235b
  err->code = code;
Packit ae235b
  err->message = g_strdup (message);
Packit ae235b
Packit ae235b
  return err;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error_free:
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Frees a #GError and associated resources.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_error_free (GError *error)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  g_free (error->message);
Packit ae235b
Packit ae235b
  g_slice_free (GError, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error_copy:
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Makes a copy of @error.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GError
Packit ae235b
 */
Packit ae235b
GError*
Packit ae235b
g_error_copy (const GError *error)
Packit ae235b
{
Packit ae235b
  GError *copy;
Packit ae235b
 
Packit ae235b
  g_return_val_if_fail (error != NULL, NULL);
Packit ae235b
  /* See g_error_new_valist for why these don't return */
Packit ae235b
  g_warn_if_fail (error->domain != 0);
Packit ae235b
  g_warn_if_fail (error->message != NULL);
Packit ae235b
Packit ae235b
  copy = g_slice_new (GError);
Packit ae235b
Packit ae235b
  *copy = *error;
Packit ae235b
Packit ae235b
  copy->message = g_strdup (error->message);
Packit ae235b
Packit ae235b
  return copy;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_error_matches:
Packit ae235b
 * @error: (nullable): a #GError
Packit ae235b
 * @domain: an error domain
Packit ae235b
 * @code: an error code
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if @error matches @domain and @code, %FALSE
Packit ae235b
 * otherwise. In particular, when @error is %NULL, %FALSE will
Packit ae235b
 * be returned.
Packit ae235b
 *
Packit ae235b
 * If @domain contains a `FAILED` (or otherwise generic) error code,
Packit ae235b
 * you should generally not check for it explicitly, but should
Packit ae235b
 * instead treat any not-explicitly-recognized error code as being
Packit ae235b
 * equivalent to the `FAILED` code. This way, if the domain is
Packit ae235b
 * extended in the future to provide a more specific error code for
Packit ae235b
 * a certain case, your code will still work.
Packit ae235b
 *
Packit ae235b
 * Returns: whether @error has @domain and @code
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_error_matches (const GError *error,
Packit ae235b
                 GQuark        domain,
Packit ae235b
                 gint          code)
Packit ae235b
{
Packit ae235b
  return error &&
Packit ae235b
    error->domain == domain &&
Packit ae235b
    error->code == code;
Packit ae235b
}
Packit ae235b
Packit ae235b
#define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
Packit ae235b
               "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
Packit ae235b
               "The overwriting error message was: %s"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_set_error:
Packit ae235b
 * @err: (out callee-allocates) (optional): a return location for a #GError
Packit ae235b
 * @domain: error domain
Packit ae235b
 * @code: error code
Packit ae235b
 * @format: printf()-style format
Packit ae235b
 * @...: args for @format
Packit ae235b
 *
Packit ae235b
 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
Packit ae235b
 * must be %NULL. A new #GError is created and assigned to *@err.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_set_error (GError      **err,
Packit ae235b
             GQuark        domain,
Packit ae235b
             gint          code,
Packit ae235b
             const gchar  *format,
Packit ae235b
             ...)
Packit ae235b
{
Packit ae235b
  GError *new;
Packit ae235b
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  if (err == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  new = g_error_new_valist (domain, code, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  if (*err == NULL)
Packit ae235b
    *err = new;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
Packit ae235b
      g_error_free (new);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_set_error_literal:
Packit ae235b
 * @err: (out callee-allocates) (optional): a return location for a #GError
Packit ae235b
 * @domain: error domain
Packit ae235b
 * @code: error code
Packit ae235b
 * @message: error message
Packit ae235b
 *
Packit ae235b
 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
Packit ae235b
 * must be %NULL. A new #GError is created and assigned to *@err.
Packit ae235b
 * Unlike g_set_error(), @message is not a printf()-style format string.
Packit ae235b
 * Use this function if @message contains text you don't have control over,
Packit ae235b
 * that could include printf() escape sequences.
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_set_error_literal (GError      **err,
Packit ae235b
                     GQuark        domain,
Packit ae235b
                     gint          code,
Packit ae235b
                     const gchar  *message)
Packit ae235b
{
Packit ae235b
  if (err == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (*err == NULL)
Packit ae235b
    *err = g_error_new_literal (domain, code, message);
Packit ae235b
  else
Packit ae235b
    g_warning (ERROR_OVERWRITTEN_WARNING, message);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_propagate_error:
Packit ae235b
 * @dest: (out callee-allocates) (optional) (nullable): error return location
Packit ae235b
 * @src: (transfer full): error to move into the return location
Packit ae235b
 *
Packit ae235b
 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
Packit ae235b
 * The error variable @dest points to must be %NULL.
Packit ae235b
 *
Packit ae235b
 * @src must be non-%NULL.
Packit ae235b
 *
Packit ae235b
 * Note that @src is no longer valid after this call. If you want
Packit ae235b
 * to keep using the same GError*, you need to set it to %NULL
Packit ae235b
 * after calling this function on it.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_propagate_error (GError **dest,
Packit ae235b
		   GError  *src)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (src != NULL);
Packit ae235b
 
Packit ae235b
  if (dest == NULL)
Packit ae235b
    {
Packit ae235b
      if (src)
Packit ae235b
        g_error_free (src);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (*dest != NULL)
Packit ae235b
        {
Packit ae235b
          g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
Packit ae235b
          g_error_free (src);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        *dest = src;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_clear_error:
Packit ae235b
 * @err: a #GError return location
Packit ae235b
 *
Packit ae235b
 * If @err or *@err is %NULL, does nothing. Otherwise,
Packit ae235b
 * calls g_error_free() on *@err and sets *@err to %NULL.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_clear_error (GError **err)
Packit ae235b
{
Packit ae235b
  if (err && *err)
Packit ae235b
    {
Packit ae235b
      g_error_free (*err);
Packit ae235b
      *err = NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
G_GNUC_PRINTF(2, 0)
Packit ae235b
static void
Packit ae235b
g_error_add_prefix (gchar       **string,
Packit ae235b
                    const gchar  *format,
Packit ae235b
                    va_list       ap)
Packit ae235b
{
Packit ae235b
  gchar *oldstring;
Packit ae235b
  gchar *prefix;
Packit ae235b
Packit ae235b
  prefix = g_strdup_vprintf (format, ap);
Packit ae235b
  oldstring = *string;
Packit ae235b
  *string = g_strconcat (prefix, oldstring, NULL);
Packit ae235b
  g_free (oldstring);
Packit ae235b
  g_free (prefix);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_prefix_error:
Packit ae235b
 * @err: (inout) (optional) (nullable): a return location for a #GError
Packit ae235b
 * @format: printf()-style format string
Packit ae235b
 * @...: arguments to @format
Packit ae235b
 *
Packit ae235b
 * Formats a string according to @format and prefix it to an existing
Packit ae235b
 * error message. If @err is %NULL (ie: no error variable) then do
Packit ae235b
 * nothing.
Packit ae235b
 *
Packit ae235b
 * If *@err is %NULL (ie: an error variable is present but there is no
Packit ae235b
 * error condition) then also do nothing.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_prefix_error (GError      **err,
Packit ae235b
                const gchar  *format,
Packit ae235b
                ...)
Packit ae235b
{
Packit ae235b
  if (err && *err)
Packit ae235b
    {
Packit ae235b
      va_list ap;
Packit ae235b
Packit ae235b
      va_start (ap, format);
Packit ae235b
      g_error_add_prefix (&(*err)->message, format, ap);
Packit ae235b
      va_end (ap);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_propagate_prefixed_error:
Packit ae235b
 * @dest: error return location
Packit ae235b
 * @src: error to move into the return location
Packit ae235b
 * @format: printf()-style format string
Packit ae235b
 * @...: arguments to @format
Packit ae235b
 *
Packit ae235b
 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
Packit ae235b
 * *@dest must be %NULL. After the move, add a prefix as with
Packit ae235b
 * g_prefix_error().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_propagate_prefixed_error (GError      **dest,
Packit ae235b
                            GError       *src,
Packit ae235b
                            const gchar  *format,
Packit ae235b
                            ...)
Packit ae235b
{
Packit ae235b
  g_propagate_error (dest, src);
Packit ae235b
Packit ae235b
  if (dest && *dest)
Packit ae235b
    {
Packit ae235b
      va_list ap;
Packit ae235b
Packit ae235b
      va_start (ap, format);
Packit ae235b
      g_error_add_prefix (&(*dest)->message, format, ap);
Packit ae235b
      va_end (ap);
Packit ae235b
    }
Packit ae235b
}