I've no idea here...
[gtkD.git] / src / glib / ErrorG.d
blob08dbafb7e39ef986c77e516ba000239e805c42bc
1 /*
2 * This file is part of duit.
4 * duit is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * duit 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
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = glib-Error-Reporting.html
26 * outPack = glib
27 * outFile = ErrorG
28 * strct = GError
29 * realStrct=
30 * ctorStrct=
31 * clss = ErrorG
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_error_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * - glib.Quark
45 * - glib.Str
46 * structWrap:
47 * local aliases:
50 module glib.ErrorG;
52 private import glib.glibtypes;
54 private import lib.glib;
56 private import glib.Quark;
57 private import glib.Str;
59 /**
60 * Description
61 * GLib provides a standard method of reporting errors from a called function to
62 * the calling code. (This is the same problem solved by exceptions in other
63 * languages.) It's important to understand that this method is both a
64 * data type (the GError object) and a set of
65 * rules. If you use GError incorrectly, then your code will not
66 * properly interoperate with other code that uses GError, and users of your API
67 * will probably get confused.
68 * First and foremost: GError should only be used to report
69 * recoverable runtime errors, never to report programming errors. If
70 * the programmer has screwed up, then you should use g_warning(),
71 * g_return_if_fail(), g_assert(), g_error(), or some similar facility.
72 * (Incidentally, remember that the g_error() function should
73 * only be used for programming errors, it should not be used
74 * to print any error reportable via GError.)
75 * Examples of recoverable runtime errors are "file not found" or "failed to parse
76 * input." Examples of programming errors are "NULL passed to strcmp()" or
77 * "attempted to free the same pointer twice." These two kinds of errors are
78 * fundamentally different: runtime errors should be handled or reported to the
79 * user, programming errors should be eliminated by fixing the bug in the program.
80 * This is why most functions in GLib and GTK+ do not use the GError facility.
81 * Functions that can fail take a return location for a GError as their last argument.
82 * For example:
83 * gboolean g_file_get_contents (const gchar *filename,
84 * gchar **contents,
85 * gsize *length,
86 * GError **error);
87 * If you pass a non-NULL value for the error argument, it should
88 * point to a location where an error can be placed. For example:
89 * gchar *contents;
90 * GError *err = NULL;
91 * g_file_get_contents ("foo.txt", contents, NULL, err);
92 * g_assert ((contents == NULL err != NULL) || (contents != NULL err == NULL));
93 * if (err != NULL)
94 * {
95 * /+* Report error to user, and free error +/
96 * g_assert (contents == NULL);
97 * fprintf (stderr, "Unable to read file: %s\n", err->message);
98 * g_error_free (err);
99 * }
100 * else
102 * /+* Use file contents +/
103 * g_assert (contents != NULL);
105 * Note that err != NULL in this example is a
106 * reliable indicator of whether
107 * g_file_get_contents() failed. Additionally, g_file_get_contents() returns
108 * a boolean which indicates whether it was successful.
109 * Because g_file_get_contents() returns FALSE on failure, if you are only
110 * interested in whether it failed and don't need to display an error message, you
111 * can pass NULL for the error argument:
112 * if (g_file_get_contents ("foo.txt", contents, NULL, NULL)) /+* ignore errors +/
113 * /+* no error occurred +/ ;
114 * else
115 * /+* error +/ ;
116 * The GError object contains three fields: domain indicates
117 * the module the error-reporting function is located in, code
118 * indicates the specific error that occurred, and message is a
119 * user-readable error message with as many details as possible. Several functions
120 * are provided to deal with an error received from a called function:
121 * g_error_matches() returns TRUE if the error matches a given domain and code,
122 * g_propagate_error() copies an error into an error location (so the calling
123 * function will receive it), and g_clear_error() clears an error location by
124 * freeing the error and resetting the location to NULL. To display an error to the
125 * user, simply display error->message, perhaps along with
126 * additional context known only to the calling function (the file being opened, or
127 * whatever -- though in the g_file_get_contents() case,
128 * error->message already contains a filename).
129 * When implementing a function that can report errors, the basic tool is
130 * g_set_error(). Typically, if a fatal error occurs you want to g_set_error(),
131 * then return immediately. g_set_error() does nothing if the error location passed
132 * to it is NULL. Here's an example:
133 * gint
134 * foo_open_file (GError **error)
136 * gint fd;
137 * fd = open ("file.txt", O_RDONLY);
138 * if (fd < 0)
140 * g_set_error (error,
141 * FOO_ERROR, /+* error domain +/
142 * FOO_ERROR_BLAH, /+* error code +/
143 * "Failed to open file: %s", /+* error message format string +/
144 * g_strerror (errno));
145 * return -1;
147 * else
148 * return fd;
150 * Things are somewhat more complicated if you yourself call another function that
151 * can report a GError. If the sub-function indicates fatal errors in some way
152 * other than reporting a GError, such as by returning TRUE on success, you can
153 * simply do the following:
154 * gboolean
155 * my_function_that_can_fail (GError **err)
157 * g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
158 * if (!sub_function_that_can_fail (err))
160 * /+* assert that error was set by the sub-function +/
161 * g_assert (err == NULL || *err != NULL);
162 * return FALSE;
164 * /+* otherwise continue, no error occurred +/
165 * g_assert (err == NULL || *err == NULL);
167 * If the sub-function does not indicate errors other than by reporting a GError,
168 * you need to create a temporary GError since the passed-in one may be NULL.
169 * g_propagate_error() is intended for use in this case.
170 * gboolean
171 * my_function_that_can_fail (GError **err)
173 * GError *tmp_error;
174 * g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
175 * tmp_error = NULL;
176 * sub_function_that_can_fail (tmp_error);
177 * if (tmp_error != NULL)
179 * /+* store tmp_error in err, if err != NULL,
180 * * otherwise call g_error_free() on tmp_error
181 * +/
182 * g_propagate_error (err, tmp_error);
183 * return FALSE;
185 * /+* otherwise continue, no error occurred +/
187 * Error pileups are always a bug. For example, this code is incorrect:
188 * gboolean
189 * my_function_that_can_fail (GError **err)
191 * GError *tmp_error;
192 * g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
193 * tmp_error = NULL;
194 * sub_function_that_can_fail (tmp_error);
195 * other_function_that_can_fail (tmp_error);
196 * if (tmp_error != NULL)
198 * g_propagate_error (err, tmp_error);
199 * return FALSE;
202 * tmp_error should be checked immediately after
203 * sub_function_that_can_fail(), and either cleared or propagated upward. The rule
204 * is: after each error, you must either handle the error, or return it to the
205 * calling function. Note that passing NULL for the error location is the
206 * equivalent of handling an error by always doing nothing about it. So the
207 * following code is fine, assuming errors in sub_function_that_can_fail() are not
208 * fatal to my_function_that_can_fail():
209 * gboolean
210 * my_function_that_can_fail (GError **err)
212 * GError *tmp_error;
213 * g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
214 * sub_function_that_can_fail (NULL); /+* ignore errors +/
215 * tmp_error = NULL;
216 * other_function_that_can_fail (tmp_error);
217 * if (tmp_error != NULL)
219 * g_propagate_error (err, tmp_error);
220 * return FALSE;
223 * Note that passing NULL for the error location ignores
224 * errors; it's equivalent to try { sub_function_that_can_fail(); } catch
225 * (...) {} in C++. It does not mean to leave errors
226 * unhandled; it means to handle them by doing nothing.
227 * Error domains and codes are conventionally named as follows:
228 * The error domain is called
229 * <NAMESPACE>_<MODULE>_ERROR, for example
230 * G_EXEC_ERROR or G_THREAD_ERROR.
231 * The error codes are in an enumeration called
232 * <Namespace><Module>Error; for example,
233 * GThreadError or GSpawnError.
234 * Members of the error code enumeration are called <NAMESPACE>_<MODULE>_ERROR_<CODE>, for example G_SPAWN_ERROR_FORK or G_THREAD_ERROR_AGAIN.
235 * If there's a "generic" or "unknown" error code for unrecoverable errors it
236 * doesn't make sense to distinguish with specific codes, it should be called
237 * <NAMESPACE>_<MODULE>_ERROR_FAILED, for
238 * example G_SPAWN_ERROR_FAILED or G_THREAD_ERROR_FAILED.
239 * Summary of rules for use of GError:
240 * Do not report programming errors via GError.
241 * The last argument of a function that returns an error should be a
242 * location where a GError can be placed (i.e. "GError** error"). If
243 * GError is used with varargs, the GError** should be the last
244 * argument before the "...".
245 * The caller may pass NULL for the GError** if they are not interested
246 * in details of the exact error that occurred.
247 * If NULL is passed for the GError** argument, then errors should
248 * not be returned to the caller, but your function should still
249 * abort and return if an error occurs. That is, control flow should
250 * not be affected by whether the caller wants to get a GError.
251 * If a GError is reported, then your function by definition
252 * had a fatal failure and did not complete whatever it was supposed
253 * to do. If the failure was not fatal, then you handled it
254 * and you should not report it. If it was fatal, then you must report it
255 * and discontinue whatever you were doing immediately.
256 * A GError* must be initialized to NULL before passing its address to
257 * a function that can report errors.
258 * "Piling up" errors is always a bug. That is, if you assign a new
259 * GError to a GError* that is non-NULL, thus overwriting the previous
260 * error, it indicates that you should have aborted the operation instead
261 * of continuing. If you were able to continue, you should have cleared
262 * the previous error with g_clear_error(). g_set_error() will complain
263 * if you pile up errors.
264 * By convention, if you return a boolean value indicating success
265 * then TRUE means success and FALSE means failure. If FALSE is returned,
266 * the error must be set to a non-NULL value.
267 * A NULL return value is also frequently used to mean that an error
268 * occurred. You should make clear in your documentation whether NULL is
269 * a valid return value in non-error cases; if NULL is a valid value,
270 * then users must check whether an error was returned to see if the
271 * function succeeded.
272 * When implementing a function that can report errors, you may want to
273 * add a check at the top of your function that the error return location
274 * is either NULL or contains a NULL error
275 * (e.g. g_return_if_fail (error == NULL || *error ==
276 * NULL);).
278 public class ErrorG
281 /** the main Gtk struct */
282 protected GError* gError;
285 public GError* getErrorGStruct()
287 return gError;
291 /** the main Gtk struct as a void* */
292 protected void* getStruct()
294 return cast(void*)gError;
298 * Sets our main struct and passes it to the parent class
300 public this (GError* gError)
302 this.gError = gError;
310 * Creates a new GError with the given domain and code,
311 * and a message formatted with format.
312 * domain:
313 * error domain
314 * code:
315 * error code
316 * format:
317 * printf()-style format for error message
318 * ...:
319 * parameters for message format
320 * Returns:
321 * a new GError
323 public this (GQuark domain, int code, char[] format, ... )
325 // GError* g_error_new (GQuark domain, gint code, const gchar *format, ...);
326 this(cast(GError*)g_error_new(domain, code, Str.toStringz(format)) );
330 * Creates a new GError; unlike g_error_new(), message is not
331 * a printf()-style format string. Use this
332 * function if message contains text you don't have control over,
333 * that could include printf() escape sequences.
334 * domain:
335 * error domain
336 * code:
337 * error code
338 * message:
339 * error message
340 * Returns:
341 * a new GError
343 public this (GQuark domain, int code, char[] message)
345 // GError* g_error_new_literal (GQuark domain, gint code, const gchar *message);
346 this(cast(GError*)g_error_new_literal(domain, code, Str.toStringz(message)) );
350 * Frees a GError and associated resources.
351 * error:
352 * a GError
354 public void free()
356 // void g_error_free (GError *error);
357 g_error_free(gError);
361 * Makes a copy of error.
362 * error:
363 * a GError
364 * Returns:
365 * a new GError
367 public GError* copy()
369 // GError* g_error_copy (const GError *error);
370 return g_error_copy(gError);
374 * Returns TRUE if error matches domain and code, FALSE
375 * otherwise.
376 * error:
377 * a GError
378 * domain:
379 * an error domain
380 * code:
381 * an error code
382 * Returns:
383 * whether error has domain and code
385 public int matches(GQuark domain, int code)
387 // gboolean g_error_matches (const GError *error, GQuark domain, gint code);
388 return g_error_matches(gError, domain, code);
392 * Does nothing if err is NULL; if err is non-NULL, then *err must
393 * be NULL. A new GError is created and assigned to *err.
394 * err:
395 * a return location for a GError, or NULL
396 * domain:
397 * error domain
398 * code:
399 * error code
400 * format:
401 * printf()-style format
402 * ...:
403 * args for format
405 public static void gSetError(GError** err, GQuark domain, int code, char[] format, ... )
407 // void g_set_error (GError **err, GQuark domain, gint code, const gchar *format, ...);
408 g_set_error(err, domain, code, Str.toStringz(format));
412 * If dest is NULL, free src; otherwise,
413 * moves src into *dest. *dest must be NULL.
414 * dest:
415 * error return location
416 * src:
417 * error to move into the return location
419 public static void gPropagateError(GError** dest, GError* src)
421 // void g_propagate_error (GError **dest, GError *src);
422 g_propagate_error(dest, src);
426 * If err is NULL, does nothing. If err is non-NULL,
427 * calls g_error_free() on *err and sets *err to NULL.
428 * err:
429 * a GError return location
431 public static void gClearError(GError** err)
433 // void g_clear_error (GError **err);
434 g_clear_error(err);