mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-28 06:56:16 +01:00
gerror: Simplify error construction
Factor out the GError creation to a common function. When adding a support for extended error types, this will limit the number of places where these errors are allocated.
This commit is contained in:
parent
d48e22e464
commit
b715e4c9d0
@ -382,6 +382,20 @@
|
|||||||
#include "gstrfuncs.h"
|
#include "gstrfuncs.h"
|
||||||
#include "gtestutils.h"
|
#include "gtestutils.h"
|
||||||
|
|
||||||
|
static GError *
|
||||||
|
g_error_new_steal (GQuark domain,
|
||||||
|
gint code,
|
||||||
|
gchar *message)
|
||||||
|
{
|
||||||
|
GError *error = g_slice_new (GError);
|
||||||
|
|
||||||
|
error->domain = domain;
|
||||||
|
error->code = code;
|
||||||
|
error->message = message;
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_error_new_valist:
|
* g_error_new_valist:
|
||||||
* @domain: error domain
|
* @domain: error domain
|
||||||
@ -402,8 +416,6 @@ g_error_new_valist (GQuark domain,
|
|||||||
const gchar *format,
|
const gchar *format,
|
||||||
va_list args)
|
va_list args)
|
||||||
{
|
{
|
||||||
GError *error;
|
|
||||||
|
|
||||||
/* Historically, GError allowed this (although it was never meant to work),
|
/* Historically, GError allowed this (although it was never meant to work),
|
||||||
* and it has significant use in the wild, which g_return_val_if_fail
|
* and it has significant use in the wild, which g_return_val_if_fail
|
||||||
* would break. It should maybe g_return_val_if_fail in GLib 4.
|
* would break. It should maybe g_return_val_if_fail in GLib 4.
|
||||||
@ -412,13 +424,7 @@ g_error_new_valist (GQuark domain,
|
|||||||
g_warn_if_fail (domain != 0);
|
g_warn_if_fail (domain != 0);
|
||||||
g_warn_if_fail (format != NULL);
|
g_warn_if_fail (format != NULL);
|
||||||
|
|
||||||
error = g_slice_new (GError);
|
return g_error_new_steal (domain, code, g_strdup_vprintf (format, args));
|
||||||
|
|
||||||
error->domain = domain;
|
|
||||||
error->code = code;
|
|
||||||
error->message = g_strdup_vprintf (format, args);
|
|
||||||
|
|
||||||
return error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -470,18 +476,10 @@ g_error_new_literal (GQuark domain,
|
|||||||
gint code,
|
gint code,
|
||||||
const gchar *message)
|
const gchar *message)
|
||||||
{
|
{
|
||||||
GError* err;
|
|
||||||
|
|
||||||
g_return_val_if_fail (message != NULL, NULL);
|
g_return_val_if_fail (message != NULL, NULL);
|
||||||
g_return_val_if_fail (domain != 0, NULL);
|
g_return_val_if_fail (domain != 0, NULL);
|
||||||
|
|
||||||
err = g_slice_new (GError);
|
return g_error_new_steal (domain, code, g_strdup (message));
|
||||||
|
|
||||||
err->domain = domain;
|
|
||||||
err->code = code;
|
|
||||||
err->message = g_strdup (message);
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -511,20 +509,14 @@ g_error_free (GError *error)
|
|||||||
GError*
|
GError*
|
||||||
g_error_copy (const GError *error)
|
g_error_copy (const GError *error)
|
||||||
{
|
{
|
||||||
GError *copy;
|
|
||||||
|
|
||||||
g_return_val_if_fail (error != NULL, NULL);
|
g_return_val_if_fail (error != NULL, NULL);
|
||||||
/* See g_error_new_valist for why these don't return */
|
/* See g_error_new_valist for why these don't return */
|
||||||
g_warn_if_fail (error->domain != 0);
|
g_warn_if_fail (error->domain != 0);
|
||||||
g_warn_if_fail (error->message != NULL);
|
g_warn_if_fail (error->message != NULL);
|
||||||
|
|
||||||
copy = g_slice_new (GError);
|
return g_error_new_steal (error->domain,
|
||||||
|
error->code,
|
||||||
*copy = *error;
|
g_strdup (error->message));
|
||||||
|
|
||||||
copy->message = g_strdup (error->message);
|
|
||||||
|
|
||||||
return copy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user