Glib::Error, again (more comments, please)



Thanks for your comments on Glib::Error; i committed the basic stuff last
week, and added some more whiz-bang features this weekend, and i wouldn't mind
more comments on them.  :-)  This stuff is in CVS HEAD.

It is now possible to register your own error domains from Perl code.  This is
most handy if you have a pure-perl Glib::Object implementation which needs to
throw errors, or if you just want to start using this stuff because it's cool.

You can create new errors with Glib::Error::new(), and pass them to croak() or
die(), or just call Glib::Error::throw(), which does that for you.

GLib provides g_error_matches() for checking if a GError matches something you
expect; i've mapped this as Glib::Error::matches(), with similar semantics. 
It also allows undef, as a nicety.

Here's an example.

  package Mup::Thing;

  use Glib;

  # create an enum for the error codes...
  Glib::Type->register_enum ('Mup::ThingErrors',
                             qw(flip flop fly die));
  # now register a new error domain using those error codes.
  Glib::Error::register ('Mup::Thing::Error', 'Mup::ThingErrors');

  sub do_something_cool {
      ...
      Mup::Thing::Error->throw ('flop', "Something terrible this way flops")
          if something_terrible_flopped;
      ...
  }

  package main;

  eval {
      ...
      Mup::Thing::do_something_cool (...);
      ...
  };
  if (Glib::Error::matches ($@, 'Mup::Thing::Error', 'flop')) {
      recover_from_a_flop ();
  } elsif ($@) {
      die $@;
  }


There is also a function to parse a perl data structure into a C GError, for
wrapping libraries which need that sort of thing (e.g., testing functions in
Glib::GConf).

And, as i have a private project which defines a dozen or so error domains in
C, i thought it would be handy to add the error domain registration stuff to
Gtk2::CodeGen to allow automation.  The slightly dirty bit is that if the
third column in the maps file is GError, the meanings of the first two columns
are now different.  You can see what i mean in Gtk2/maps-2.0 and
Gtk2/maps-2.3.

-- 
muppet <scott at asofyet dot org>



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