1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
34 * @domain: error domain
36 * @format: printf()-style format for error message
37 * @args: #va_list of parameters for the message format
39 * Creates a new #GError with the given @domain and @code,
40 * and a message formatted with @format.
42 * Returns: a new #GError
47 g_error_new_valist (GQuark domain,
54 error = g_slice_new (GError);
56 error->domain = domain;
58 error->message = g_strdup_vprintf (format, args);
65 * @domain: error domain
67 * @format: printf()-style format for error message
68 * @Varargs: parameters for message format
70 * Creates a new #GError with the given @domain and @code,
71 * and a message formatted with @format.
73 * Return value: a new #GError
76 g_error_new (GQuark domain,
84 g_return_val_if_fail (format != NULL, NULL);
85 g_return_val_if_fail (domain != 0, NULL);
87 va_start (args, format);
88 error = g_error_new_valist (domain, code, format, args);
95 * g_error_new_literal:
96 * @domain: error domain
98 * @message: error message
100 * Creates a new #GError; unlike g_error_new(), @message is
101 * not a printf()-style format string. Use this function if
102 * @message contains text you don't have control over,
103 * that could include printf() escape sequences.
105 * Return value: a new #GError
108 g_error_new_literal (GQuark domain,
110 const gchar *message)
114 g_return_val_if_fail (message != NULL, NULL);
115 g_return_val_if_fail (domain != 0, NULL);
117 err = g_slice_new (GError);
119 err->domain = domain;
121 err->message = g_strdup (message);
130 * Frees a #GError and associated resources.
133 g_error_free (GError *error)
135 g_return_if_fail (error != NULL);
137 g_free (error->message);
139 g_slice_free (GError, error);
146 * Makes a copy of @error.
148 * Return value: a new #GError
151 g_error_copy (const GError *error)
155 g_return_val_if_fail (error != NULL, NULL);
157 copy = g_slice_new (GError);
161 copy->message = g_strdup (error->message);
168 * @error: a #GError or %NULL
169 * @domain: an error domain
170 * @code: an error code
172 * Returns %TRUE if @error matches @domain and @code, %FALSE
173 * otherwise. In particular, when @error is %NULL, %FALSE will
176 * Return value: whether @error has @domain and @code
179 g_error_matches (const GError *error,
184 error->domain == domain &&
188 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
189 "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
190 "The overwriting error message was: %s"
194 * @err: a return location for a #GError, or %NULL
195 * @domain: error domain
197 * @format: printf()-style format
198 * @Varargs: args for @format
200 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
201 * must be %NULL. A new #GError is created and assigned to *@err.
204 g_set_error (GError **err,
217 va_start (args, format);
218 new = g_error_new_valist (domain, code, format, args);
224 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
228 * g_set_error_literal:
229 * @err: a return location for a #GError, or %NULL
230 * @domain: error domain
232 * @message: error message
234 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
235 * must be %NULL. A new #GError is created and assigned to *@err.
236 * Unlike g_set_error(), @message is not a printf()-style format string.
237 * Use this function if @message contains text you don't have control over,
238 * that could include printf() escape sequences.
243 g_set_error_literal (GError **err,
246 const gchar *message)
253 new = g_error_new_literal (domain, code, message);
257 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
262 * @dest: error return location
263 * @src: error to move into the return location
265 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
266 * The error variable @dest points to must be %NULL.
269 g_propagate_error (GError **dest,
272 g_return_if_fail (src != NULL);
283 g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
291 * @err: a #GError return location
293 * If @err is %NULL, does nothing. If @err is non-%NULL,
294 * calls g_error_free() on *@err and sets *@err to %NULL.
297 g_clear_error (GError **err)
307 g_error_add_prefix (gchar **string,
314 prefix = g_strdup_vprintf (format, ap);
316 *string = g_strconcat (prefix, oldstring, NULL);
323 * @err: a return location for a #GError, or %NULL
324 * @format: printf()-style format string
325 * @...: arguments to @format
327 * Formats a string according to @format and
328 * prefix it to an existing error message. If
329 * @err is %NULL (ie: no error variable) then do
332 * If *@err is %NULL (ie: an error variable is
333 * present but there is no error condition) then
334 * also do nothing. Whether or not it makes
335 * sense to take advantage of this feature is up
341 g_prefix_error (GError **err,
349 va_start (ap, format);
350 g_error_add_prefix (&(*err)->message, format, ap);
356 * g_propagate_prefixed_error:
357 * @dest: error return location
358 * @src: error to move into the return location
359 * @format: printf()-style format string
360 * @...: arguments to @format
362 * If @dest is %NULL, free @src; otherwise,
363 * moves @src into *@dest. *@dest must be %NULL.
364 * After the move, add a prefix as with
370 g_propagate_prefixed_error (GError **dest,
375 g_propagate_error (dest, src);
381 va_start (ap, format);
382 g_error_add_prefix (&(*dest)->message, format, ap);