Fix the wrong-category schema test
[glib.git] / glib / gerror.c
blob1f39739100d1df80bb800ac8a9f98166900b55e5
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/.
27 #include "config.h"
29 #include "gerror.h"
31 #include "gstrfuncs.h"
32 #include "gtestutils.h"
34 /**
35 * g_error_new_valist:
36 * @domain: error domain
37 * @code: error code
38 * @format: printf()-style format for error message
39 * @args: #va_list of parameters for the message format
41 * Creates a new #GError with the given @domain and @code,
42 * and a message formatted with @format.
44 * Returns: a new #GError
46 * Since: 2.22
48 GError*
49 g_error_new_valist (GQuark domain,
50 gint code,
51 const gchar *format,
52 va_list args)
54 GError *error;
56 error = g_slice_new (GError);
58 error->domain = domain;
59 error->code = code;
60 error->message = g_strdup_vprintf (format, args);
62 return error;
65 /**
66 * g_error_new:
67 * @domain: error domain
68 * @code: error code
69 * @format: printf()-style format for error message
70 * @Varargs: parameters for message format
72 * Creates a new #GError with the given @domain and @code,
73 * and a message formatted with @format.
75 * Return value: a new #GError
77 GError*
78 g_error_new (GQuark domain,
79 gint code,
80 const gchar *format,
81 ...)
83 GError* error;
84 va_list args;
86 g_return_val_if_fail (format != NULL, NULL);
87 g_return_val_if_fail (domain != 0, NULL);
89 va_start (args, format);
90 error = g_error_new_valist (domain, code, format, args);
91 va_end (args);
93 return error;
96 /**
97 * g_error_new_literal:
98 * @domain: error domain
99 * @code: error code
100 * @message: error message
102 * Creates a new #GError; unlike g_error_new(), @message is
103 * not a printf()-style format string. Use this function if
104 * @message contains text you don't have control over,
105 * that could include printf() escape sequences.
107 * Return value: a new #GError
109 GError*
110 g_error_new_literal (GQuark domain,
111 gint code,
112 const gchar *message)
114 GError* err;
116 g_return_val_if_fail (message != NULL, NULL);
117 g_return_val_if_fail (domain != 0, NULL);
119 err = g_slice_new (GError);
121 err->domain = domain;
122 err->code = code;
123 err->message = g_strdup (message);
125 return err;
129 * g_error_free:
130 * @error: a #GError
132 * Frees a #GError and associated resources.
134 void
135 g_error_free (GError *error)
137 g_return_if_fail (error != NULL);
139 g_free (error->message);
141 g_slice_free (GError, error);
145 * g_error_copy:
146 * @error: a #GError
148 * Makes a copy of @error.
150 * Return value: a new #GError
152 GError*
153 g_error_copy (const GError *error)
155 GError *copy;
157 g_return_val_if_fail (error != NULL, NULL);
159 copy = g_slice_new (GError);
161 *copy = *error;
163 copy->message = g_strdup (error->message);
165 return copy;
169 * g_error_matches:
170 * @error: a #GError or %NULL
171 * @domain: an error domain
172 * @code: an error code
174 * Returns %TRUE if @error matches @domain and @code, %FALSE
175 * otherwise. In particular, when @error is %NULL, %FALSE will
176 * be returned.
178 * Return value: whether @error has @domain and @code
180 gboolean
181 g_error_matches (const GError *error,
182 GQuark domain,
183 gint code)
185 return error &&
186 error->domain == domain &&
187 error->code == code;
190 #define ERROR_OVERWRITTEN_WARNING "GError set over the top of a previous GError or uninitialized memory.\n" \
191 "This indicates a bug in someone's code. You must ensure an error is NULL before it's set.\n" \
192 "The overwriting error message was: %s"
195 * g_set_error:
196 * @err: a return location for a #GError, or %NULL
197 * @domain: error domain
198 * @code: error code
199 * @format: printf()-style format
200 * @Varargs: args for @format
202 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
203 * must be %NULL. A new #GError is created and assigned to *@err.
205 void
206 g_set_error (GError **err,
207 GQuark domain,
208 gint code,
209 const gchar *format,
210 ...)
212 GError *new;
214 va_list args;
216 if (err == NULL)
217 return;
219 va_start (args, format);
220 new = g_error_new_valist (domain, code, format, args);
221 va_end (args);
223 if (*err == NULL)
224 *err = new;
225 else
226 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
230 * g_set_error_literal:
231 * @err: a return location for a #GError, or %NULL
232 * @domain: error domain
233 * @code: error code
234 * @message: error message
236 * Does nothing if @err is %NULL; if @err is non-%NULL, then *@err
237 * must be %NULL. A new #GError is created and assigned to *@err.
238 * Unlike g_set_error(), @message is not a printf()-style format string.
239 * Use this function if @message contains text you don't have control over,
240 * that could include printf() escape sequences.
242 * Since: 2.18
244 void
245 g_set_error_literal (GError **err,
246 GQuark domain,
247 gint code,
248 const gchar *message)
250 GError *new;
252 if (err == NULL)
253 return;
255 new = g_error_new_literal (domain, code, message);
256 if (*err == NULL)
257 *err = new;
258 else
259 g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
263 * g_propagate_error:
264 * @dest: error return location
265 * @src: error to move into the return location
267 * If @dest is %NULL, free @src; otherwise, moves @src into *@dest.
268 * The error variable @dest points to must be %NULL.
270 void
271 g_propagate_error (GError **dest,
272 GError *src)
274 g_return_if_fail (src != NULL);
276 if (dest == NULL)
278 if (src)
279 g_error_free (src);
280 return;
282 else
284 if (*dest != NULL)
285 g_warning (ERROR_OVERWRITTEN_WARNING, src->message);
286 else
287 *dest = src;
292 * g_clear_error:
293 * @err: a #GError return location
295 * If @err is %NULL, does nothing. If @err is non-%NULL,
296 * calls g_error_free() on *@err and sets *@err to %NULL.
298 void
299 g_clear_error (GError **err)
301 if (err && *err)
303 g_error_free (*err);
304 *err = NULL;
308 static void
309 g_error_add_prefix (gchar **string,
310 const gchar *format,
311 va_list ap)
313 gchar *oldstring;
314 gchar *prefix;
316 prefix = g_strdup_vprintf (format, ap);
317 oldstring = *string;
318 *string = g_strconcat (prefix, oldstring, NULL);
319 g_free (oldstring);
320 g_free (prefix);
324 * g_prefix_error:
325 * @err: a return location for a #GError, or %NULL
326 * @format: printf()-style format string
327 * @...: arguments to @format
329 * Formats a string according to @format and
330 * prefix it to an existing error message. If
331 * @err is %NULL (ie: no error variable) then do
332 * nothing.
334 * If *@err is %NULL (ie: an error variable is
335 * present but there is no error condition) then
336 * also do nothing. Whether or not it makes
337 * sense to take advantage of this feature is up
338 * to you.
340 * Since: 2.16
342 void
343 g_prefix_error (GError **err,
344 const gchar *format,
345 ...)
347 if (err && *err)
349 va_list ap;
351 va_start (ap, format);
352 g_error_add_prefix (&(*err)->message, format, ap);
353 va_end (ap);
358 * g_propagate_prefixed_error:
359 * @dest: error return location
360 * @src: error to move into the return location
361 * @format: printf()-style format string
362 * @...: arguments to @format
364 * If @dest is %NULL, free @src; otherwise,
365 * moves @src into *@dest. *@dest must be %NULL.
366 * After the move, add a prefix as with
367 * g_prefix_error().
369 * Since: 2.16
371 void
372 g_propagate_prefixed_error (GError **dest,
373 GError *src,
374 const gchar *format,
375 ...)
377 g_propagate_error (dest, src);
379 if (dest && *dest)
381 va_list ap;
383 va_start (ap, format);
384 g_error_add_prefix (&(*dest)->message, format, ap);
385 va_end (ap);