1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 /* 25 * Conversion parameters: 26 * inFile = glib-Error-Reporting.html 27 * outPack = glib 28 * outFile = ErrorG 29 * strct = GError 30 * realStrct= 31 * ctorStrct= 32 * clss = ErrorG 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_error_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * - glib.Str 47 * structWrap: 48 * - GError* -> ErrorG 49 * module aliases: 50 * local aliases: 51 * overrides: 52 */ 53 54 module glib.ErrorG; 55 56 public import gtkc.glibtypes; 57 58 private import gtkc.glib; 59 private import glib.ConstructionException; 60 61 62 private import glib.Str; 63 64 65 66 67 /** 68 * Description 69 * GLib provides a standard method of reporting errors from a called function to 70 * the calling code. (This is the same problem solved by exceptions in other 71 * languages.) It's important to understand that this method is both a 72 * data type (the GError object) and a set of 73 * rules. If you use GError incorrectly, then your code will not 74 * properly interoperate with other code that uses GError, and users of your API 75 * will probably get confused. 76 * First and foremost: GError should only be used to report 77 * recoverable runtime errors, never to report programming errors. If 78 * the programmer has screwed up, then you should use g_warning(), 79 * g_return_if_fail(), g_assert(), g_error(), or some similar facility. 80 * (Incidentally, remember that the g_error() function should 81 * only be used for programming errors, it should not be used 82 * to print any error reportable via GError.) 83 * Examples of recoverable runtime errors are "file not found" or "failed to parse 84 * input." Examples of programming errors are "NULL passed to strcmp()" or 85 * "attempted to free the same pointer twice." These two kinds of errors are 86 * fundamentally different: runtime errors should be handled or reported to the 87 * user, programming errors should be eliminated by fixing the bug in the program. 88 * This is why most functions in GLib and GTK+ do not use the GError facility. 89 * Functions that can fail take a return location for a GError as their last argument. 90 * For example: 91 * $(DDOC_COMMENT example) 92 * If you pass a non-NULL value for the error argument, it should 93 * point to a location where an error can be placed. For example: 94 * $(DDOC_COMMENT example) 95 * Note that err != NULL in this example is a 96 * reliable indicator of whether 97 * g_file_get_contents() failed. Additionally, g_file_get_contents() returns 98 * a boolean which indicates whether it was successful. 99 * Because g_file_get_contents() returns FALSE on failure, if you are only 100 * interested in whether it failed and don't need to display an error message, you 101 * can pass NULL for the error argument: 102 * $(DDOC_COMMENT example) 103 * The GError object contains three fields: domain indicates 104 * the module the error-reporting function is located in, code 105 * indicates the specific error that occurred, and message is a 106 * user-readable error message with as many details as possible. Several functions 107 * are provided to deal with an error received from a called function: 108 * g_error_matches() returns TRUE if the error matches a given domain and code, 109 * g_propagate_error() copies an error into an error location (so the calling 110 * function will receive it), and g_clear_error() clears an error location by 111 * freeing the error and resetting the location to NULL. To display an error to the 112 * user, simply display error->message, perhaps along with 113 * additional context known only to the calling function (the file being opened, or 114 * whatever -- though in the g_file_get_contents() case, 115 * error->message already contains a filename). 116 * When implementing a function that can report errors, the basic tool is 117 * g_set_error(). Typically, if a fatal error occurs you want to g_set_error(), 118 * then return immediately. g_set_error() does nothing if the error location passed 119 * to it is NULL. Here's an example: 120 * $(DDOC_COMMENT example) 121 * Things are somewhat more complicated if you yourself call another function that 122 * can report a GError. If the sub-function indicates fatal errors in some way 123 * other than reporting a GError, such as by returning TRUE on success, you can 124 * simply do the following: 125 * $(DDOC_COMMENT example) 126 * If the sub-function does not indicate errors other than by reporting a GError, 127 * you need to create a temporary GError since the passed-in one may be NULL. 128 * g_propagate_error() is intended for use in this case. 129 * $(DDOC_COMMENT example) 130 * Error pileups are always a bug. For example, this code is incorrect: 131 * $(DDOC_COMMENT example) 132 * tmp_error should be checked immediately after 133 * sub_function_that_can_fail(), and either cleared or propagated upward. The rule 134 * is: after each error, you must either handle the error, or return it to the 135 * calling function. Note that passing NULL for the error location is the 136 * equivalent of handling an error by always doing nothing about it. So the 137 * following code is fine, assuming errors in sub_function_that_can_fail() are not 138 * fatal to my_function_that_can_fail(): 139 * $(DDOC_COMMENT example) 140 * Note that passing NULL for the error location ignores 141 * errors; it's equivalent to try { sub_function_that_can_fail(); } catch 142 * (...) {} in C++. It does not mean to leave errors 143 * unhandled; it means to handle them by doing nothing. 144 * Error domains and codes are conventionally named as follows: 145 * The error domain is called 146 * <NAMESPACE>_<MODULE>_ERROR, for example 147 * G_SPAWN_ERROR or G_THREAD_ERROR: 148 * $(DDOC_COMMENT example) 149 * The quark function for the error domain is called <namespace>_<module>_error_quark, for example g_spawn_error_quark() or %g_thread_error_quark(). 150 * The error codes are in an enumeration called 151 * <Namespace><Module>Error; for example, 152 * GThreadError or GSpawnError. 153 * Members of the error code enumeration are called <NAMESPACE>_<MODULE>_ERROR_<CODE>, for example G_SPAWN_ERROR_FORK or G_THREAD_ERROR_AGAIN. 154 * If there's a "generic" or "unknown" error code for unrecoverable errors it 155 * doesn't make sense to distinguish with specific codes, it should be called 156 * <NAMESPACE>_<MODULE>_ERROR_FAILED, for 157 * example G_SPAWN_ERROR_FAILED or G_THREAD_ERROR_FAILED. 158 * Summary of rules for use of GError: 159 * Do not report programming errors via GError. 160 * The last argument of a function that returns an error should be a 161 * location where a GError can be placed (i.e. "GError** error"). If 162 * GError is used with varargs, the GError** should be the last 163 * argument before the "...". 164 * The caller may pass NULL for the GError** if they are not interested 165 * in details of the exact error that occurred. 166 * If NULL is passed for the GError** argument, then errors should 167 * not be returned to the caller, but your function should still 168 * abort and return if an error occurs. That is, control flow should 169 * not be affected by whether the caller wants to get a GError. 170 * If a GError is reported, then your function by definition 171 * had a fatal failure and did not complete whatever it was supposed 172 * to do. If the failure was not fatal, then you handled it 173 * and you should not report it. If it was fatal, then you must report it 174 * and discontinue whatever you were doing immediately. 175 * A GError* must be initialized to NULL before passing its address to 176 * a function that can report errors. 177 * "Piling up" errors is always a bug. That is, if you assign a new 178 * GError to a GError* that is non-NULL, thus overwriting the previous 179 * error, it indicates that you should have aborted the operation instead 180 * of continuing. If you were able to continue, you should have cleared 181 * the previous error with g_clear_error(). g_set_error() will complain 182 * if you pile up errors. 183 * By convention, if you return a boolean value indicating success 184 * then TRUE means success and FALSE means failure. If FALSE is returned, 185 * the error must be set to a non-NULL value. 186 * A NULL return value is also frequently used to mean that an error 187 * occurred. You should make clear in your documentation whether NULL is 188 * a valid return value in non-error cases; if NULL is a valid value, 189 * then users must check whether an error was returned to see if the 190 * function succeeded. 191 * When implementing a function that can report errors, you may want to 192 * add a check at the top of your function that the error return location 193 * is either NULL or contains a NULL error 194 * (e.g. g_return_if_fail (error == NULL || *error == 195 * NULL);). 196 */ 197 public class ErrorG 198 { 199 200 /** the main Gtk struct */ 201 protected GError* gError; 202 203 204 public GError* getErrorGStruct() 205 { 206 return gError; 207 } 208 209 210 /** the main Gtk struct as a void* */ 211 protected void* getStruct() 212 { 213 return cast(void*)gError; 214 } 215 216 /** 217 * Sets our main struct and passes it to the parent class 218 */ 219 public this (GError* gError) 220 { 221 this.gError = gError; 222 } 223 224 /** 225 */ 226 227 /** 228 * Creates a new GError; unlike g_error_new(), message is 229 * not a printf()-style format string. Use this function if 230 * message contains text you don't have control over, 231 * that could include printf() escape sequences. 232 * Params: 233 * domain = error domain 234 * code = error code 235 * message = error message 236 * Throws: ConstructionException GTK+ fails to create the object. 237 */ 238 public this (GQuark domain, int code, string message) 239 { 240 // GError * g_error_new_literal (GQuark domain, gint code, const gchar *message); 241 auto p = g_error_new_literal(domain, code, Str.toStringz(message)); 242 if(p is null) 243 { 244 throw new ConstructionException("null returned by g_error_new_literal(domain, code, Str.toStringz(message))"); 245 } 246 this(cast(GError*) p); 247 } 248 249 /** 250 * Creates a new GError with the given domain and code, 251 * and a message formatted with format. 252 * Since 2.22 253 * Params: 254 * domain = error domain 255 * code = error code 256 * format = printf()-style format for error message 257 * args = va_list of parameters for the message format 258 * Throws: ConstructionException GTK+ fails to create the object. 259 */ 260 public this (GQuark domain, int code, string format, void* args) 261 { 262 // GError * g_error_new_valist (GQuark domain, gint code, const gchar *format, va_list args); 263 auto p = g_error_new_valist(domain, code, Str.toStringz(format), args); 264 if(p is null) 265 { 266 throw new ConstructionException("null returned by g_error_new_valist(domain, code, Str.toStringz(format), args)"); 267 } 268 this(cast(GError*) p); 269 } 270 271 /** 272 * Frees a GError and associated resources. 273 */ 274 public void free() 275 { 276 // void g_error_free (GError *error); 277 g_error_free(gError); 278 } 279 280 /** 281 * Makes a copy of error. 282 * Returns: a new GError 283 */ 284 public ErrorG copy() 285 { 286 // GError * g_error_copy (const GError *error); 287 auto p = g_error_copy(gError); 288 289 if(p is null) 290 { 291 return null; 292 } 293 294 return new ErrorG(cast(GError*) p); 295 } 296 297 /** 298 * Returns TRUE if error matches domain and code, FALSE 299 * otherwise. In particular, when error is NULL, FALSE will 300 * be returned. 301 * Params: 302 * domain = an error domain 303 * code = an error code 304 * Returns: whether error has domain and code 305 */ 306 public int matches(GQuark domain, int code) 307 { 308 // gboolean g_error_matches (const GError *error, GQuark domain, gint code); 309 return g_error_matches(gError, domain, code); 310 } 311 312 /** 313 * Does nothing if err is NULL; if err is non-NULL, then *err 314 * must be NULL. A new GError is created and assigned to *err. 315 * Unlike g_set_error(), message is not a printf()-style format string. 316 * Use this function if message contains text you don't have control over, 317 * that could include printf() escape sequences. 318 * Since 2.18 319 * Params: 320 * err = a return location for a GError, or NULL 321 * domain = error domain 322 * code = error code 323 * message = error message 324 */ 325 public static void gSetErrorLiteral(out ErrorG err, GQuark domain, int code, string message) 326 { 327 // void g_set_error_literal (GError **err, GQuark domain, gint code, const gchar *message); 328 GError* outerr = null; 329 330 g_set_error_literal(&outerr, domain, code, Str.toStringz(message)); 331 332 err = new ErrorG(outerr); 333 } 334 335 /** 336 * If dest is NULL, free src; otherwise, moves src into *dest. 337 * The error variable dest points to must be NULL. 338 * Params: 339 * dest = error return location 340 * src = error to move into the return location 341 */ 342 public static void gPropagateError(out ErrorG dest, ErrorG src) 343 { 344 // void g_propagate_error (GError **dest, GError *src); 345 GError* outdest = null; 346 347 g_propagate_error(&outdest, (src is null) ? null : src.getErrorGStruct()); 348 349 dest = new ErrorG(outdest); 350 } 351 352 /** 353 * If err is NULL, does nothing. If err is non-NULL, 354 * calls g_error_free() on *err and sets *err to NULL. 355 * Params: 356 * err = a GError return location 357 */ 358 public static void gClearError(ref ErrorG err) 359 { 360 // void g_clear_error (GError **err); 361 GError* outerr = (err is null) ? null : err.getErrorGStruct(); 362 363 g_clear_error(&outerr); 364 365 err = new ErrorG(outerr); 366 } 367 }