From 05ec2ec30f171faa1309fa2c2ac4f5c894a35dcf Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 21 Oct 2013 13:55:43 +0100 Subject: [PATCH 11/11] Add non-GError-based async method errors --- dbus/dbus-glib.h | 3 +++ dbus/dbus-gobject.c | 37 +++++++++++++++++++++++++++++++++++++ test/core/my-object.c | 14 ++++++++++++++ test/core/not-quite-gdbus.c | 15 +++++++++++++++ test/core/test-service-glib.xml | 6 ++++++ 5 files changed, 75 insertions(+) diff --git a/dbus/dbus-glib.h b/dbus/dbus-glib.h index 1674244..d40268c 100644 --- a/dbus/dbus-glib.h +++ b/dbus/dbus-glib.h @@ -352,6 +352,9 @@ void dbus_g_method_return_variants (DBusGMethodInvocation *con GVariant *tuple); void dbus_g_method_return_error (DBusGMethodInvocation *context, const GError *error); +void dbus_g_method_return_named_error (DBusGMethodInvocation *context, + const gchar *error_name, + const gchar *error_message); DBusGConnection * dbus_g_method_invocation_get_g_connection (DBusGMethodInvocation *context); diff --git a/dbus/dbus-gobject.c b/dbus/dbus-gobject.c index 56727e2..7a4945e 100644 --- a/dbus/dbus-gobject.c +++ b/dbus/dbus-gobject.c @@ -3366,6 +3366,43 @@ out: } /** + * dbus_g_method_return_named_error: + * @context: the method context + * @error_name: the machine-readable D-Bus error name + * @error_message: the human-readable message + * + * Send a error message for a given method invocation. + * This function also frees the sending context. + */ +void +dbus_g_method_return_named_error (DBusGMethodInvocation *context, + const gchar *error_name, + const gchar *error_message) +{ + DBusMessage *reply; + + g_return_if_fail (context != NULL); + g_return_if_fail (error_name != NULL); + g_return_if_fail (error_message != NULL); + + /* See comment in dbus_g_method_return */ + if (!context->send_reply) + goto out; + + reply = dbus_message_new_error ( + dbus_g_message_get_message (context->message), error_name, + error_message); + connection_send_or_die ( + dbus_g_connection_get_connection (context->connection), reply); + dbus_message_unref (reply); + +out: + dbus_g_connection_unref (context->connection); + dbus_g_message_unref (context->message); + g_free (context); +} + +/** * dbus_g_method_invocation_get_g_connection: * @context: the method context * diff --git a/test/core/my-object.c b/test/core/my-object.c index 8442927..08021e3 100644 --- a/test/core/my-object.c +++ b/test/core/my-object.c @@ -8,6 +8,11 @@ static void my_object_async_increment_gvariant (MyObject *obj, guint32 x, DBusGMethodInvocation *context); +static void my_object_async_throw_named_error (MyObject *obj, + const gchar *name, + const gchar *message, + DBusGMethodInvocation *context); + #include "test-service-glib-glue.h" void @@ -881,6 +886,15 @@ my_object_async_throw_error (MyObject *obj, DBusGMethodInvocation *context) g_idle_add ((GSourceFunc) do_async_error, data); } +static void +my_object_async_throw_named_error (MyObject *obj, + const gchar *name, + const gchar *message, + DBusGMethodInvocation *context) +{ + dbus_g_method_return_named_error (context, name, message); +} + gboolean my_object_unsafe_disable_legacy_property_access (MyObject *obj, GError **error) diff --git a/test/core/not-quite-gdbus.c b/test/core/not-quite-gdbus.c index e9e1bf3..fa8d94b 100644 --- a/test/core/not-quite-gdbus.c +++ b/test/core/not-quite-gdbus.c @@ -195,6 +195,21 @@ test_failure (Fixture *f, g_assert (v == NULL); g_clear_object (&result); g_clear_error (&error); + + dbus_g_connection_call_async (f->bus, f->unique_name, "/foo", IFACE, + "AsyncThrowNamedError", + g_variant_new_parsed ("(\"com.example.Badness\", \"something failed\")"), + NULL, G_DBUS_CALL_FLAGS_NONE, + -1, NULL, tp_tests_result_ready_cb, &result); + tp_tests_run_until_result (&result); + + v = dbus_g_connection_call_finish (result, &error); + g_assert_error (error, DBUS_GERROR, DBUS_GERROR_REMOTE_EXCEPTION); + g_assert_cmpstr (dbus_g_error_get_name (error), ==, "com.example.Badness"); + g_assert_cmpstr (error->message, ==, "something failed"); + g_assert (v == NULL); + g_clear_object (&result); + g_clear_error (&error); } static void diff --git a/test/core/test-service-glib.xml b/test/core/test-service-glib.xml index ca8f9e9..0caa792 100644 --- a/test/core/test-service-glib.xml +++ b/test/core/test-service-glib.xml @@ -132,6 +132,12 @@ + + + + + + -- 1.8.4.rc3