Re: [Vala] Introspection for exception instances



On Monday, 13 November 2017, 14:25:04 GMT, Michael Gratton <mike vee net> wrote: 

For example, given an instance of a 
IOError.CANCELLABLE, I'd like to be able to come up with a string 
representation of it as "IOError.CANCELLABLE". At the moment the best I 
can get is the quark and code as strings, i.e. "g-io-error" and "32" 
(or whatever).

I hope this gets you a step further, but I've not applied this with GLib's cancellable.
The general pattern is:

void main () {
    try {
        test ();
    } catch (IOError error) {
        print (error.message + "\n");
    }
}

void test () throws IOError {
    throw new IOError.CANCELLED ("This test has been cancelled");
}

Just compile with valac --pkg gio-2.0 cancellable_error.vala and you will see
the message. 

So you probably want to be looking at:
https://valadoc.org/gio-2.0/GLib.Cancellable.set_error_if_cancelled.html
https://developer.gnome.org/gio/stable/GCancellable.html#g-cancellable-set-error-if-cancelled

From the Valadoc representation is looks as though the GError is taken as an out parameter,
but for this method it should be an in parameter. So there could be something wrong with the bindings there.

Relatedly, since the error's code is an enum, it would be handy to be 
able to use it in a switch statement, e.g.:

switch (io_error.code) {
case IOError.CANCELLABLE:
    ...
    break;
}

I'm not sure about this one. I've tried:

case IOError.CANCELLED.code

and had a look at:

https://valadoc.org/gio-2.0/GLib.IOError.from_errno.html
https://developer.gnome.org/glib/stable/glib-Error-Reporting.html#g-error-matches
https://developer.gnome.org/gio/stable/gio-GIOError.html#GIOErrorEnum

but nothing obvious worked.

I assumed CANCELLABLE was a typo.

Al


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