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. --- glib/dbus-glib.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 13 deletions(-) (limited to 'glib/dbus-glib.c') diff --git a/glib/dbus-glib.c b/glib/dbus-glib.c index 7065b668..3153deef 100644 --- a/glib/dbus-glib.c +++ b/glib/dbus-glib.c @@ -26,6 +26,7 @@ #include #include "dbus-gtest.h" #include "dbus-gutils.h" +#include #include #define _(x) dgettext (GETTEXT_PACKAGE, x) @@ -148,26 +149,87 @@ dbus_g_error_quark (void) return quark; } +#include "dbus-glib-error-switch.h" /** - * Set a GError return location from a DBusError. + * Set a GError return location from a D-BUS error name and message. + * This function should only be used in the implementation of service + * methods. * - * @todo expand the DBUS_GERROR enum and take advantage of it here - * * @param gerror location to store a GError, or #NULL - * @param derror the DBusError + * @param name the D-BUS error name + * @param msg the D-BUS error detailed message */ void -dbus_set_g_error (GError **gerror, - DBusError *derror) +dbus_g_error_set (GError **gerror, + const char *name, + const char *msg) { - g_return_if_fail (derror != NULL); - g_return_if_fail (dbus_error_is_set (derror)); - - g_set_error (gerror, DBUS_GERROR, - DBUS_GERROR_FAILED, - _("D-BUS error %s: %s"), - derror->name, derror->message); + int code; + g_return_if_fail (name != NULL); + g_return_if_fail (msg != NULL); + + code = dbus_error_to_gerror_code (name); + if (code == DBUS_GERROR_REMOTE_EXCEPTION) + g_set_error (gerror, DBUS_GERROR, + code, + "%s%c%s", + msg, + '\0', + name); + else + g_set_error (gerror, DBUS_GERROR, + code, + "%s", + msg); +} + +/** + * Determine whether D-BUS error name for a remote exception matches + * the given name. This function is intended to be invoked on a + * GError returned from an invocation of a remote method, e.g. via + * dbus_g_proxy_end_call. It will silently return FALSE for errors + * which are not remote D-BUS exceptions (i.e. with a domain other + * than DBUS_GERROR or a code other than + * DBUS_GERROR_REMOTE_EXCEPTION). + * + * @param gerror the GError given from the remote method + * @param name the D-BUS error name + * @param msg the D-BUS error detailed message + * @returns TRUE iff the remote error has the given name + */ +gboolean +dbus_g_error_has_name (GError *error, const char *name) +{ + g_return_val_if_fail (error != NULL, FALSE); + + if (error->domain != DBUS_GERROR + || error->code != DBUS_GERROR_REMOTE_EXCEPTION) + return FALSE; + + return !strcmp (dbus_g_error_get_name (error), name); +} + +/** + * Return the D-BUS name for a remote exception. + * This function may only be invoked on a GError returned from an + * invocation of a remote method, e.g. via dbus_g_proxy_end_call. + * Moreover, you must ensure that the error's domain is DBUS_GERROR, + * and the code is DBUS_GERROR_REMOTE_EXCEPTION. + * + * @param gerror the GError given from the remote method + * @param name the D-BUS error name + * @param msg the D-BUS error detailed message + * @returns the D-BUS error name + */ +const char * +dbus_g_error_get_name (GError *error) +{ + g_return_val_if_fail (error != NULL, NULL); + g_return_val_if_fail (error->domain == DBUS_GERROR, NULL); + g_return_val_if_fail (error->code == DBUS_GERROR_REMOTE_EXCEPTION, NULL); + + return error->message + strlen (error->message) + 1; } /** @@ -395,7 +457,28 @@ dbus_g_pending_call_cancel (DBusGPendingCall *call) gboolean _dbus_glib_test (const char *test_data_dir) { + DBusError err; + GError *gerror = NULL; + + dbus_error_init (&err); + dbus_set_error_const (&err, DBUS_ERROR_NO_MEMORY, "Out of memory!"); + + dbus_g_error_set (&gerror, err.name, err.message); + g_assert (gerror != NULL); + g_assert (gerror->domain == DBUS_GERROR); + g_assert (gerror->code == DBUS_GERROR_NO_MEMORY); + g_assert (!strcmp (gerror->message, "Out of memory!")); + dbus_error_init (&err); + g_clear_error (&gerror); + + dbus_g_error_set (&gerror, "com.example.Foo.BlahFailed", "blah failed"); + g_assert (gerror != NULL); + g_assert (gerror->domain == DBUS_GERROR); + g_assert (gerror->code == DBUS_GERROR_REMOTE_EXCEPTION); + g_assert (dbus_g_error_has_name (gerror, "com.example.Foo.BlahFailed")); + g_assert (!strcmp (gerror->message, "blah failed")); + return TRUE; } -- cgit