]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Issue a warning on DBus methods which are not throwing an Error
authorDr. Michael Lauer <mickey@vanille-media.de>
Sun, 11 Feb 2018 16:18:35 +0000 (17:18 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 11 Feb 2018 20:06:40 +0000 (21:06 +0100)
It is recommended to throw "GLib.Error" or "GLib.DBusError, GLib.IOError".
This will be turned into an error at some point.

https://bugzilla.gnome.org/show_bug.cgi?id=792277

tests/Makefile.am
tests/dbus/bug792277.vala [new file with mode: 0644]
vala/valamethod.vala

index 7b9d57c537ecf576cbcd08296aa87f5d5b51e603..decdb14e24132d37bdccde5f404d8c425a1e053f 100644 (file)
@@ -363,6 +363,7 @@ TESTS = \
        dbus/bug596862.vala \
        dbus/bug602003.test \
        dbus/bug782719.test \
+       dbus/bug792277.vala \
        dbus/rawvariants.test \
        gir/bug651773.test \
        gir/bug667751.test \
diff --git a/tests/dbus/bug792277.vala b/tests/dbus/bug792277.vala
new file mode 100644 (file)
index 0000000..4c8c08e
--- /dev/null
@@ -0,0 +1,21 @@
+[DBus (name = "org.example.IFoo")]
+public interface IFoo : Object {
+       public abstract void method0 () throws Error;
+       public abstract void method1 () throws DBusError, IOError;
+       [DBus (visible = false)]
+       public abstract void method2 ();
+}
+
+[DBus (name = "org.example.Foo")]
+public class Foo : Object {
+       public void method0 () throws Error {
+       }
+       public void method1 () throws DBusError, IOError {
+       }
+       [DBus (visible = false)]
+       public void method2 () {
+       }
+}
+
+void main () {
+}
index 8af2f667a56b3fd5cdeac8dfce72b4d94eecbebc..133bca52e17d68ab19605efa1e2a4f0250c680a4 100644 (file)
@@ -856,6 +856,35 @@ public class Vala.Method : Subroutine, Callable {
                        }
                }
 
+               // check that DBus methods at least throw "GLib.Error" or "GLib.DBusError, GLib.IOError"
+               if (parent_symbol is ObjectTypeSymbol && parent_symbol.get_attribute ("DBus") != null) {
+                       Attribute? dbus_attr = get_attribute ("DBus");
+                       if (dbus_attr == null || dbus_attr.get_bool ("visible", true)) {
+                               bool throws_gerror = false;
+                               bool throws_gioerror = false;
+                               bool throws_gdbuserror = false;
+                               foreach (DataType error_type in get_error_types ()) {
+                                       if (!(error_type is ErrorType)) {
+                                               continue;
+                                       }
+                                       unowned ErrorDomain? error_domain = ((ErrorType) error_type).error_domain;
+                                       if (error_domain == null) {
+                                               throws_gerror = true;
+                                               break;
+                                       }
+                                       string? full_error_domain = error_domain.get_full_name ();
+                                       if (full_error_domain == "GLib.IOError") {
+                                               throws_gioerror = true;
+                                       } else if (full_error_domain == "GLib.DBusError") {
+                                               throws_gdbuserror = true;
+                                       }
+                               }
+                               if (!throws_gerror && !(throws_gioerror && throws_gdbuserror)) {
+                                       Report.warning (source_reference, "DBus methods are recommended to throw at least `GLib.Error' or `GLib.DBusError, GLib.IOError'");
+                               }
+                       }
+               }
+
                if (is_possible_entry_point (context)) {
                        if (context.entry_point != null) {
                                error = true;