From df901b528bc1e1edd96e9e91b94c9c9b795b8ffd Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sun, 26 Jun 2005 17:02:09 +0000 Subject: 2005-06-26 Colin Walters * glib/dbus-glib.c (dbus_set_g_error): Delete. (dbus_g_error_set): New public function from its ashes; used by both service-side method implementation and GLib bindings internals. (dbus_g_error_has_name, dbus_g_error_get_name): New function. (_dbus_glib_test): Add some tests. * test/glib/test-dbus-glib.c (main): Test dbus_g_error_has_name. * test/glib/test-service-glib.c (my_object_throw_error): Use dbus_g_error_set. * glib/dbus-gobject.c (gerror_to_dbus_error_message): Handle errors thrown by dbus_g_error_set. * glib/dbus-gmain.c (dbus_g_bus_get): Change to dbus_g_error_set. * glib/dbus-gparser.c (validate_signature): Ditto. * glib/dbus-gproxy.c (dbus_g_proxy_new_for_name_owner) (dbus_g_proxy_end_call_internal): Ditto. * glib/Makefile.am: Generate dbus-glib-error-switch.h, which converts DBUS_ERROR_x to DBUS_GERROR_x. (libdbus_glib_1_la_SOURCES, BUILT_SOURCES, CLEANFILES): Add it. * doc/TODO: Remove error TODO. * doc/dbus-tutorial.xml: Update with documentation about error handling. * dbus/make-dbus-glib-error-enum.sh: Tighten up regexp to make sure we only change DBUS_ERROR to DBUS_GERROR, not all ERROR to GERROR. Also add DBUS_GERROR_REMOTE_EXCEPTION. --- doc/dbus-tutorial.xml | 102 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 34 deletions(-) (limited to 'doc/dbus-tutorial.xml') diff --git a/doc/dbus-tutorial.xml b/doc/dbus-tutorial.xml index 304d0d70..bd6313c7 100644 --- a/doc/dbus-tutorial.xml +++ b/doc/dbus-tutorial.xml @@ -772,8 +772,13 @@ main (int argc, char **argv) if (!dbus_g_proxy_call (proxy, "ListNames", &error, G_TYPE_INVALID, G_TYPE_STRV, &name_list, G_TYPE_INVALID)) { - g_printerr ("Failed to complete ListNames call: %s\n", - error->message); + /* Just do demonstrate remote exceptions versus regular GError */ + if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) + g_printerr ("Caught remote method exception %s: %s", + dbus_g_error_get_name (error), + error->message); + else + g_printerr ("Error: %s\n", error->message); g_error_free (error); exit (1); } @@ -830,10 +835,60 @@ main (int argc, char **argv) You may connect to signals using dbus_g_proxy_add_signal and - dbus_g_proxy_connect_signal. At the - moment, dbus_g_proxy_add_signal requires - the D-BUS types of the remote object; this will likely be - changed later. + dbus_g_proxy_connect_signal. You must + invoke dbus_g_proxy_add_signal to specify + the signature of your signal handlers; you may then invoke + dbus_g_proxy_connect_signal multiple times. + + + Note that it will often be the case that there is no builtin + marshaller for the type signature of a remote signal. In that + case, you must generate a marshaller yourself by using + glib-genmarshal, and then register + it using dbus_g_object_register_marshaller. + + + + Error handling and remote exceptions + + All of the GLib binding methods such as + dbus_g_proxy_end_call return a + GError. This GError can + represent two different things: + + + + An internal D-BUS error, such as an out-of-memory + condition, an I/O error, or a network timeout. Errors + generated by the D-BUS library itself have the domain + DBUS_GERROR, and a corresponding code + such as DBUS_GERROR_NO_MEMORY. It will + not be typical for applications to handle these errors + specifically. + + + + + A remote D-BUS exception, thrown by the peer, bus, or + service. D-BUS remote exceptions have both a textual + "name" and a "message". The GLib bindings store this + information in the GError, but some + special rules apply. + + + The set error will have the domain + DBUS_GERROR as above, and will also + have the code + DBUS_GERROR_REMOTE_EXCEPTION. In order + to access the remote exception name, you must use a + special accessor, such as + dbus_g_error_has_name or + dbus_g_error_get_name. The remote + exception detailed message is accessible via the regular + GError message member. + + + @@ -850,10 +905,7 @@ main (int argc, char **argv) G_TYPE_INVALID, DBUS_TYPE_G_UCHAR_ARRAY, &arr, G_TYPE_INVALID)) { - g_printerr ("Failed to complete Foobar: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } g_assert (arr != NULL); printf ("got back %u values", arr->len); @@ -875,10 +927,7 @@ main (int argc, char **argv) DBUS_TYPE_G_STRING_STRING_HASH, hash, G_TYPE_INVALID, G_TYPE_UINT, &ret, G_TYPE_INVALID)) { - g_printerr ("Failed to complete HashSize: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } g_assert (ret == 2); g_hash_table_destroy (hash); @@ -899,10 +948,7 @@ main (int argc, char **argv) G_TYPE_STRING, &strret, G_TYPE_INVALID)) { - g_printerr ("Failed to complete GetStuff: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } printf ("%s %s", boolret ? "TRUE" : "FALSE", strret); g_free (strret); @@ -933,10 +979,7 @@ main (int argc, char **argv) G_TYPE_INVALID, G_TYPE_INVALID)) { - g_printerr ("Failed to complete TwoStrArrays: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } g_strfreev (strs_dynamic); @@ -958,10 +1001,7 @@ main (int argc, char **argv) G_TYPE_STRV, &strs, G_TYPE_INVALID)) { - g_printerr ("Failed to complete GetStrs: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } for (strs_p = strs; *strs_p; strs_p++) printf ("got string: \"%s\"", *strs_p); @@ -983,10 +1023,7 @@ main (int argc, char **argv) G_TYPE_VALUE, &val, G_TYPE_INVALID, G_TYPE_INVALID)) { - g_printerr ("Failed to complete SendVariant: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } g_assert (ret == 2); g_value_unset (&val); @@ -1003,10 +1040,7 @@ main (int argc, char **argv) if (!dbus_g_proxy_call (proxy, "GetVariant", &error, G_TYPE_INVALID, G_TYPE_VALUE, &val, G_TYPE_INVALID)) { - g_printerr ("Failed to complete GetVariant: %s\n", - error->message); - g_error_free (error); - exit (1); + /* Handle error */ } if (G_VALUE_TYPE (&val) == G_TYPE_STRING) printf ("%s\n", g_value_get_string (&val)); -- cgit