From 94c1682b5e9e1f1ee04d80e362f6a8d7a2495327 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 28 Nov 2008 16:23:43 +0000 Subject: [PATCH] Bug 16776: teach dbus_g_method_return_error about DBUS_GERROR Code called by dbus_g_method_return_error assumes that errors in the domain DBUS_GERROR always have the code DBUS_GERROR_REMOTE_EXCEPTION. This is clearly not true, and it would be nice to be able to raise the "well-known" D-Bus errors from library user code. --- dbus/dbus-gobject.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 66 insertions(+), 3 deletions(-) diff --git a/dbus/dbus-gobject.c b/dbus/dbus-gobject.c index b6e72e2..f55e5cc 100644 --- a/dbus/dbus-gobject.c +++ b/dbus/dbus-gobject.c @@ -1019,9 +1019,72 @@ gerror_to_dbus_error_message (const DBusGObjectInfo *object_info, else { if (error->domain == DBUS_GERROR) - reply = dbus_message_new_error (message, - dbus_g_error_get_name (error), - error->message); + { + const gchar *name = DBUS_ERROR_FAILED; + + switch (error->code) + { + case DBUS_GERROR_FAILED: + name = DBUS_ERROR_FAILED; + break; + case DBUS_GERROR_NO_MEMORY: + name = DBUS_ERROR_NO_MEMORY; + break; + case DBUS_GERROR_SERVICE_UNKNOWN: + name = DBUS_ERROR_SERVICE_UNKNOWN; + break; + case DBUS_GERROR_NAME_HAS_NO_OWNER: + name = DBUS_ERROR_NAME_HAS_NO_OWNER; + break; + case DBUS_GERROR_NO_REPLY: + name = DBUS_ERROR_NO_REPLY; + break; + case DBUS_GERROR_IO_ERROR: + name = DBUS_ERROR_IO_ERROR; + break; + case DBUS_GERROR_BAD_ADDRESS: + name = DBUS_ERROR_BAD_ADDRESS; + break; + case DBUS_GERROR_NOT_SUPPORTED: + name = DBUS_ERROR_NOT_SUPPORTED; + break; + case DBUS_GERROR_LIMITS_EXCEEDED: + name = DBUS_ERROR_LIMITS_EXCEEDED; + break; + case DBUS_GERROR_ACCESS_DENIED: + name = DBUS_ERROR_ACCESS_DENIED; + break; + case DBUS_GERROR_AUTH_FAILED: + name = DBUS_ERROR_AUTH_FAILED; + break; + case DBUS_GERROR_NO_SERVER: + name = DBUS_ERROR_NO_SERVER; + break; + case DBUS_GERROR_TIMEOUT: + name = DBUS_ERROR_TIMEOUT; + break; + case DBUS_GERROR_NO_NETWORK: + name = DBUS_ERROR_NO_NETWORK; + break; + case DBUS_GERROR_ADDRESS_IN_USE: + name = DBUS_ERROR_ADDRESS_IN_USE; + break; + case DBUS_GERROR_DISCONNECTED: + name = DBUS_ERROR_DISCONNECTED; + break; + case DBUS_GERROR_INVALID_ARGS: + name = DBUS_ERROR_INVALID_ARGS; + break; + case DBUS_GERROR_FILE_NOT_FOUND: + name = DBUS_ERROR_FILE_NOT_FOUND; + break; + case DBUS_GERROR_REMOTE_EXCEPTION: + name = dbus_g_error_get_name (error); + break; + } + + reply = dbus_message_new_error (message, name, error->message); + } else { char *error_name; -- 1.5.6.5 From 23e9c5ce0789ab95fc4584b140ac56354d330dc5 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 28 Nov 2008 17:22:59 +0000 Subject: [PATCH] Bug #16776: add a regression test --- test/core/my-object.c | 11 +++++++++++ test/core/my-object.h | 1 + test/core/test-dbus-glib.c | 13 +++++++++++++ test/core/test-service-glib.xml | 3 +++ 4 files changed, 28 insertions(+), 0 deletions(-) diff --git a/test/core/my-object.c b/test/core/my-object.c index f508496..0b7c7a7 100644 --- a/test/core/my-object.c +++ b/test/core/my-object.c @@ -224,6 +224,17 @@ my_object_throw_error (MyObject *obj, GError **error) } gboolean +my_object_throw_not_supported (MyObject *obj, GError **error) +{ + g_set_error (error, + DBUS_GERROR, + DBUS_GERROR_NOT_SUPPORTED, + "%s", + "this method always loses"); + return FALSE; +} + +gboolean my_object_uppercase (MyObject *obj, const char *str, char **ret, GError **error) { *ret = g_ascii_strup (str, -1); diff --git a/test/core/my-object.h b/test/core/my-object.h index 75f4c46..9605184 100644 --- a/test/core/my-object.h +++ b/test/core/my-object.h @@ -49,6 +49,7 @@ gint32 my_object_increment_retval (MyObject *obj, gint32 x); gint32 my_object_increment_retval_error (MyObject *obj, gint32 x, GError **error); gboolean my_object_throw_error (MyObject *obj, GError **error); +gboolean my_object_throw_not_supported (MyObject *obj, GError **error); gboolean my_object_uppercase (MyObject *obj, const char *str, char **ret, GError **error); diff --git a/test/core/test-dbus-glib.c b/test/core/test-dbus-glib.c index f0d34f8..2cfb1a8 100644 --- a/test/core/test-dbus-glib.c +++ b/test/core/test-dbus-glib.c @@ -599,6 +599,19 @@ main (int argc, char **argv) g_print ("ThrowError failed (as expected) returned error: %s\n", error->message); g_clear_error (&error); + g_print ("Calling ThrowNotSupported\n"); + if (dbus_g_proxy_call (proxy, "ThrowNotSupported", &error, + G_TYPE_INVALID, G_TYPE_INVALID) != FALSE) + lose ("ThrowNotSupported call unexpectedly succeeded!"); + + if (error->domain != DBUS_GERROR || error->code != DBUS_GERROR_NOT_SUPPORTED) + lose ("ThrowNotSupported call returned unexpected error: %s #%u: %s", + g_quark_to_string (error->domain), error->code, + dbus_g_error_get_name (error), error->message); + + g_print ("ThrowNotSupported correctly returned error: %s\n", error->message); + g_clear_error (&error); + g_print ("Calling IncrementRetvalError (for error)\n"); error = NULL; v_UINT32_2 = 0; diff --git a/test/core/test-service-glib.xml b/test/core/test-service-glib.xml index fca02af..19a6c5c 100644 --- a/test/core/test-service-glib.xml +++ b/test/core/test-service-glib.xml @@ -27,6 +27,9 @@ + + + -- 1.5.6.5