Search code examples
javascriptgnomegiogjs

How can I check error code in Gnome/gjs/Gio


This doesn't work (nothing happens when the directory exists):

let s_dir = Gio.file_new_for_path("./S1");
try {
        s_dir.make_directory(null);
} catch(e) {
        if(e == Gio.IOErrorEnum.EXISTS)
            print(e);
}

Solution

  • Use the GLib.Error.matches() method:

    let s_dir = Gio.file_new_for_path("./S1");
    try {
            s_dir.make_directory(null);
    } catch(e) {
            if (e.matches (Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)
                print(e);
    }