gdbus: Use symbolic constants for the most common D-Bus error names

To avoid adding a large block of macros to gdbusprivate.h, I've only
added a subset of the well-known error names. I chose to draw the
line by adding constants for the errors emitted via their string names
in GDBusConnection, but not for error names that are only mentioned
in `gdbuserror.c` or in tests.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2024-05-13 13:10:15 +01:00 committed by Philip Withnall
parent 7c609f8142
commit 510d0716be
5 changed files with 43 additions and 35 deletions

View File

@ -2418,7 +2418,7 @@ name_watcher_deliver_get_name_owner_reply_unlocked (SignalData *name_watcher,
if (type == G_DBUS_MESSAGE_TYPE_ERROR)
{
if (g_strcmp0 (g_dbus_message_get_error_name (message),
"org.freedesktop.DBus.Error.NameHasNoOwner"))
DBUS_ERROR_NAME_HAS_NO_OWNER))
name_watcher_set_name_owner_unlocked (name_watcher, NULL);
/* else it's something like NoReply or AccessDenied, which tells
* us nothing - leave the owner set to whatever we most recently
@ -4715,7 +4715,7 @@ invoke_get_property_in_idle_cb (gpointer _data)
&es))
{
reply = g_dbus_message_new_method_error (data->message,
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("No such interface “org.freedesktop.DBus.Properties” on object at path %s"),
g_dbus_message_get_path (data->message));
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -4851,7 +4851,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection *connect
if (vtable == NULL)
goto out;
/* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
/* Check that the property exists - if not fail with DBUS_ERROR_INVALID_ARGS
*/
property_info = NULL;
@ -4860,7 +4860,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection *connect
if (property_info == NULL)
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("No such property “%s”"),
property_name);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -4872,7 +4872,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection *connect
if (is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("Property “%s” is not readable"),
property_name);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -4883,7 +4883,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection *connect
else if (!is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("Property “%s” is not writable"),
property_name);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -4896,14 +4896,14 @@ validate_and_maybe_schedule_property_getset (GDBusConnection *connect
{
GVariant *value;
/* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
/* Fail with DBUS_ERROR_INVALID_ARGS if the type
* of the given value is wrong
*/
g_variant_get_child (g_dbus_message_get_body (message), 2, "v", &value);
if (g_strcmp0 (g_variant_get_type_string (value), property_info->signature) != 0)
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("Error setting property “%s”: Expected type “%s” but got “%s”"),
property_name, property_info->signature,
g_variant_get_type_string (value));
@ -5000,7 +5000,7 @@ handle_getset_property (GDBusConnection *connection,
&property_name,
NULL);
/* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
/* Fail with DBUS_ERROR_INVALID_ARGS if there is
* no such interface registered
*/
ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
@ -5008,7 +5008,7 @@ handle_getset_property (GDBusConnection *connection,
{
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("No such interface “%s”"),
interface_name);
g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -5069,7 +5069,7 @@ invoke_get_all_properties_in_idle_cb (gpointer _data)
&es))
{
reply = g_dbus_message_new_method_error (data->message,
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("No such interface “org.freedesktop.DBus.Properties” on object at path %s"),
g_dbus_message_get_path (data->message));
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -5215,7 +5215,7 @@ handle_get_all_properties (GDBusConnection *connection,
"(&s)",
&interface_name);
/* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
/* Fail with DBUS_ERROR_INVALID_ARGS if there is
* no such interface registered
*/
ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
@ -5223,7 +5223,7 @@ handle_get_all_properties (GDBusConnection *connection,
{
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("No such interface “%s”"),
interface_name);
g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -5439,7 +5439,7 @@ call_in_idle_cb (gpointer user_data)
{
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation),
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("No such interface “%s” on object at path %s"),
g_dbus_method_invocation_get_interface_name (invocation),
g_dbus_method_invocation_get_object_path (invocation));
@ -5534,13 +5534,13 @@ validate_and_maybe_schedule_method_call (GDBusConnection *connection,
/* TODO: the cost of this is O(n) - it might be worth caching the result */
method_info = g_dbus_interface_info_lookup_method (interface_info, g_dbus_message_get_member (message));
/* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
/* if the method doesn't exist, return the DBUS_ERROR_UNKNOWN_METHOD
* error to the caller
*/
if (method_info == NULL)
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("No such method “%s”"),
g_dbus_message_get_member (message));
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -5561,7 +5561,7 @@ validate_and_maybe_schedule_method_call (GDBusConnection *connection,
}
/* Check that the incoming args are of the right type - if they are not, return
* the org.freedesktop.DBus.Error.InvalidArgs error to the caller
* the DBUS_ERROR_INVALID_ARGS error to the caller
*/
in_type = _g_dbus_compute_complete_signature (method_info->in_args);
if (!g_variant_is_of_type (parameters, in_type))
@ -5571,7 +5571,7 @@ validate_and_maybe_schedule_method_call (GDBusConnection *connection,
type_string = g_variant_type_dup_string (in_type);
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("Type of message, “%s”, does not match expected type “%s”"),
g_variant_get_type_string (parameters),
type_string);
@ -7196,14 +7196,14 @@ handle_subtree_method_invocation (GDBusConnection *connection,
interface_info = interfaces[n];
}
/* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
/* Fail with DBUS_ERROR_INVALID_ARGS if the user-code
* claims it won't support the interface
*/
if (interface_info == NULL)
{
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
DBUS_ERROR_INVALID_ARGS,
_("No such interface “%s”"),
interface_name);
g_dbus_connection_send_message (es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
@ -7315,7 +7315,7 @@ process_subtree_vtable_message_in_idle_cb (gpointer _data)
{
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (data->message,
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("Method “%s” on interface “%s” with signature “%s” does not exist"),
g_dbus_message_get_member (data->message),
g_dbus_message_get_interface (data->message),
@ -7546,7 +7546,7 @@ handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
if (connection->machine_id == NULL)
{
reply = g_dbus_message_new_method_error_literal (message,
"org.freedesktop.DBus.Error.Failed",
DBUS_ERROR_FAILED,
error->message);
g_error_free (error);
}
@ -7723,7 +7723,7 @@ distribute_method_call (GDBusConnection *connection,
if (object_found == TRUE)
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("No such interface “%s” on object at path %s"),
interface_name,
path);
@ -7731,7 +7731,7 @@ distribute_method_call (GDBusConnection *connection,
else
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.UnknownMethod",
DBUS_ERROR_UNKNOWN_METHOD,
_("Object does not exist at path “%s”"),
path);
}

View File

@ -35,10 +35,10 @@
static const GDBusErrorEntry g_dbus_error_entries[] =
{
{G_DBUS_ERROR_FAILED, "org.freedesktop.DBus.Error.Failed"},
{G_DBUS_ERROR_FAILED, DBUS_ERROR_FAILED},
{G_DBUS_ERROR_NO_MEMORY, "org.freedesktop.DBus.Error.NoMemory"},
{G_DBUS_ERROR_SERVICE_UNKNOWN, "org.freedesktop.DBus.Error.ServiceUnknown"},
{G_DBUS_ERROR_NAME_HAS_NO_OWNER, "org.freedesktop.DBus.Error.NameHasNoOwner"},
{G_DBUS_ERROR_NAME_HAS_NO_OWNER, DBUS_ERROR_NAME_HAS_NO_OWNER},
{G_DBUS_ERROR_NO_REPLY, "org.freedesktop.DBus.Error.NoReply"},
{G_DBUS_ERROR_IO_ERROR, "org.freedesktop.DBus.Error.IOError"},
{G_DBUS_ERROR_BAD_ADDRESS, "org.freedesktop.DBus.Error.BadAddress"},
@ -51,10 +51,10 @@ static const GDBusErrorEntry g_dbus_error_entries[] =
{G_DBUS_ERROR_NO_NETWORK, "org.freedesktop.DBus.Error.NoNetwork"},
{G_DBUS_ERROR_ADDRESS_IN_USE, "org.freedesktop.DBus.Error.AddressInUse"},
{G_DBUS_ERROR_DISCONNECTED, "org.freedesktop.DBus.Error.Disconnected"},
{G_DBUS_ERROR_INVALID_ARGS, "org.freedesktop.DBus.Error.InvalidArgs"},
{G_DBUS_ERROR_INVALID_ARGS, DBUS_ERROR_INVALID_ARGS},
{G_DBUS_ERROR_FILE_NOT_FOUND, "org.freedesktop.DBus.Error.FileNotFound"},
{G_DBUS_ERROR_FILE_EXISTS, "org.freedesktop.DBus.Error.FileExists"},
{G_DBUS_ERROR_UNKNOWN_METHOD, "org.freedesktop.DBus.Error.UnknownMethod"},
{G_DBUS_ERROR_UNKNOWN_METHOD, DBUS_ERROR_UNKNOWN_METHOD},
{G_DBUS_ERROR_TIMED_OUT, "org.freedesktop.DBus.Error.TimedOut"},
{G_DBUS_ERROR_MATCH_RULE_NOT_FOUND, "org.freedesktop.DBus.Error.MatchRuleNotFound"},
{G_DBUS_ERROR_MATCH_RULE_INVALID, "org.freedesktop.DBus.Error.MatchRuleInvalid"},

View File

@ -42,6 +42,12 @@ G_BEGIN_DECLS
#define DBUS_INTERFACE_PEER "org.freedesktop.DBus.Peer"
#define DBUS_INTERFACE_PROPERTIES "org.freedesktop.DBus.Properties"
/* Frequently-used D-Bus error names */
#define DBUS_ERROR_FAILED "org.freedesktop.DBus.Error.Failed"
#define DBUS_ERROR_INVALID_ARGS "org.freedesktop.DBus.Error.InvalidArgs"
#define DBUS_ERROR_NAME_HAS_NO_OWNER "org.freedesktop.DBus.Error.NameHasNoOwner"
#define DBUS_ERROR_UNKNOWN_METHOD "org.freedesktop.DBus.Error.UnknownMethod"
/* Owner flags */
#define DBUS_NAME_FLAG_ALLOW_REPLACEMENT 0x1 /**< Allow another service to become the primary owner if requested */
#define DBUS_NAME_FLAG_REPLACE_EXISTING 0x2 /**< Request to replace the current primary owner */

View File

@ -24,6 +24,8 @@
#include <unistd.h>
#include <string.h>
#include "gdbusprivate.h"
/* ---------------------------------------------------------------------------------------------------- */
/* Test that registered errors are properly mapped */
/* ---------------------------------------------------------------------------------------------------- */
@ -66,13 +68,13 @@ test_registered_errors (void)
*
* We just check a couple of well-known errors.
*/
check_registered_error ("org.freedesktop.DBus.Error.Failed",
check_registered_error (DBUS_ERROR_FAILED,
G_DBUS_ERROR,
G_DBUS_ERROR_FAILED);
check_registered_error ("org.freedesktop.DBus.Error.AddressInUse",
G_DBUS_ERROR,
G_DBUS_ERROR_ADDRESS_IN_USE);
check_registered_error ("org.freedesktop.DBus.Error.UnknownMethod",
check_registered_error (DBUS_ERROR_UNKNOWN_METHOD,
G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_METHOD);
check_registered_error ("org.freedesktop.DBus.Error.UnknownObject",

View File

@ -827,7 +827,7 @@ test_dispatch_thread_func (gpointer user_data)
NULL,
&error);
g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, “(s)”, does not match expected type “()”");
g_assert_cmpstr (error->message, ==, "GDBus.Error:" DBUS_ERROR_INVALID_ARGS ": Type of message, “(s)”, does not match expected type “()”");
g_error_free (error);
g_assert_null (value);
@ -840,7 +840,7 @@ test_dispatch_thread_func (gpointer user_data)
NULL,
&error);
g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method “NonExistantMethod”");
g_assert_cmpstr (error->message, ==, "GDBus.Error:" DBUS_ERROR_UNKNOWN_METHOD ": No such method “NonExistantMethod”");
g_error_free (error);
g_assert_null (value);
@ -888,7 +888,7 @@ test_dispatch_thread_func (gpointer user_data)
&error);
g_assert_null (value);
g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property “ThisDoesntExist”");
g_assert_cmpstr (error->message, ==, "GDBus.Error:" DBUS_ERROR_INVALID_ARGS ": No such property “ThisDoesntExist”");
g_error_free (error);
error = NULL;
@ -903,7 +903,7 @@ test_dispatch_thread_func (gpointer user_data)
&error);
g_assert_null (value);
g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property “NotReadable” is not readable");
g_assert_cmpstr (error->message, ==, "GDBus.Error:" DBUS_ERROR_INVALID_ARGS ": Property “NotReadable” is not readable");
g_error_free (error);
error = NULL;
@ -940,7 +940,7 @@ test_dispatch_thread_func (gpointer user_data)
&error);
g_assert_null (value);
g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property “NotWritable” is not writable");
g_assert_cmpstr (error->message, ==, "GDBus.Error:" DBUS_ERROR_INVALID_ARGS ": Property “NotWritable” is not writable");
g_error_free (error);
error = NULL;