Wrapping Errors



Dear List,

GError doesn't support error wrapping as Java does. Is GLib is purposely
avoiding it?
If not, it will become a useful feature as the number of libraries that
uses GError grows, as the feature has already been proved useful in
Java, (indicated in this article):

http://tutorials.jenkov.com/java-exception-handling/exception-wrapping.html 

GError will need an extra field for chaining up the causes. gerror.h
doesn't have any preserved bytes for an extra field. However, because
GError is always accessed by pointers and no GLib program is supposed to
statically allocate memory for GError, it should be safe to append an
field to the internal structure of GError while keeping the ABI
compatible.

Two new proposed functions:

GError * g_error_new_with_cause(GQuark domain, gint code, GError ** cause, const char* format, ....);
Returns an new GError with `cause' as its cause. `*cause' is set to NULL.

void g_set_error_with_cause(GError ** error, GQuark domain, gint code, GError ** cause, const char * format, ...);
sets *error to an error caused by *cause if error is not NULL. if error is NULL, free *cause.

g_error_free should be modified so that all the chained up errors are freed.


Example:
my_function(GError ** error) {
  GError * tmp_error = NULL;
  some_io_function(...., &tmp_error);
  if(tmp_error != NULL) {
     g_set_error_with_cause(error, MY_ERROR, MY_ERROR_NUMBER1, &tmp_error, "some random error caused by some_io_function");
     return;
  }
  /** do something else **/
}

Regards,


Yu



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]