Register secret service errors as DBus errors

* and strip remote error codes from error->message so that
   they don't clutter things.
This commit is contained in:
Stef Walter 2012-07-09 08:44:50 +02:00
parent 8339f296e8
commit 30d1337a7c
9 changed files with 84 additions and 14 deletions

View File

@ -1137,8 +1137,10 @@ on_item_load_secret (GObject *source,
}
}
if (error != NULL)
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
}
g_simple_async_result_complete (res);
g_object_unref (res);
@ -1337,8 +1339,10 @@ on_get_secrets_complete (GObject *source,
g_variant_unref (retval);
}
if (error != NULL)
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (async, error);
}
g_simple_async_result_complete (async);
g_object_unref (async);
@ -1545,10 +1549,12 @@ on_item_set_secret (GObject *source,
retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error);
if (error == NULL)
if (error == NULL) {
_secret_item_set_cached_secret (self, set->value);
else
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
}
if (retval != NULL)
g_variant_unref (retval);

View File

@ -539,8 +539,10 @@ on_get_secrets_complete (GObject *source,
GError *error = NULL;
closure->out = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error);
if (error != NULL)
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
}
g_simple_async_result_complete (res);
g_object_unref (res);
@ -896,6 +898,7 @@ on_xlock_called (GObject *source,
retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error);
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
g_simple_async_result_complete (res);
@ -1291,6 +1294,7 @@ on_delete_complete (GObject *source,
g_variant_unref (retval);
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
g_simple_async_result_complete (res);
}
@ -1519,6 +1523,7 @@ on_create_collection_called (GObject *source,
g_variant_unref (retval);
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
g_simple_async_result_complete (res);
}
@ -1784,6 +1789,7 @@ on_create_item_called (GObject *source,
g_variant_unref (retval);
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
g_simple_async_result_complete (res);
}
@ -2056,6 +2062,8 @@ secret_service_read_alias_dbus_path_finish (SecretService *self,
GVariant *retval;
retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, error);
_secret_util_strip_remote_error (error);
if (retval == NULL)
return NULL;
@ -2174,6 +2182,8 @@ secret_service_set_alias_to_dbus_path_finish (SecretService *self,
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, error);
_secret_util_strip_remote_error (error);
if (retval == NULL)
return FALSE;

View File

@ -60,6 +60,8 @@ void _secret_sync_on_result (GObject *source,
SecretPrompt * _secret_prompt_instance (SecretService *service,
const gchar *prompt_path);
void _secret_util_strip_remote_error (GError **error);
gchar * _secret_util_parent_path (const gchar *path);
gboolean _secret_util_empty_path (const gchar *path);

View File

@ -331,6 +331,7 @@ on_prompt_prompted (GObject *source,
g_clear_error (&error);
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
perform_prompt_complete (res, TRUE);
@ -377,6 +378,7 @@ on_prompt_dismissed (GObject *source,
g_clear_error (&error);
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
perform_prompt_complete (res, TRUE);
}

View File

@ -103,6 +103,7 @@
EGG_SECURE_GLIB_DEFINITIONS ();
GQuark _secret_error_quark = 0;
static const gchar *default_bus_name = SECRET_SERVICE_BUS_NAME;
enum {
@ -542,6 +543,9 @@ secret_service_class_init (SecretServiceClass *klass)
_secret_list_get_type (), G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_type_class_add_private (klass, sizeof (SecretServicePrivate));
/* Initialize this error domain, registers dbus errors */
_secret_error_quark = secret_error_get_quark ();
}
typedef struct {

View File

@ -248,6 +248,7 @@ on_service_open_session_plain (GObject *source,
g_variant_unref (response);
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
g_simple_async_result_complete (res);
}
@ -296,6 +297,7 @@ on_service_open_session_aes (GObject *source,
/* Other errors result in a failure */
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
g_simple_async_result_complete (res);
}

View File

@ -29,6 +29,9 @@ GQuark secret_error_get_quark (void) G_GNUC_CONST;
typedef enum {
SECRET_ERROR_PROTOCOL = 1,
SECRET_ERROR_IS_LOCKED = 2,
SECRET_ERROR_NO_SUCH_OBJECT = 3,
SECRET_ERROR_ALREADY_EXISTS = 4,
} SecretError;
typedef struct _SecretCollection SecretCollection;

View File

@ -35,6 +35,11 @@
* SecretError:
* @SECRET_ERROR_PROTOCOL: received an invalid data or message from the Secret
* Service
* @SECRET_ERROR_IS_LOCKED: the item or collection is locked and the operation
* cannot be performed
* @SECRET_ERROR_NO_SUCH_OBJECT: no such item or collection found in the
* Secret Service
* @SECRET_ERROR_ALREADY_EXISTS: a relevant item or collection already exists
*
* Errors returned by the Secret Service. None of the errors are appropriate
* for display to the user.
@ -77,17 +82,38 @@ _secret_list_get_type (void)
GQuark
secret_error_get_quark (void)
{
static volatile gsize initialized = 0;
static GQuark quark = 0;
static volatile gsize quark = 0;
if (g_once_init_enter (&initialized)) {
quark = g_quark_from_static_string ("secret-error");
g_once_init_leave (&initialized, 1);
}
static const GDBusErrorEntry entries[] = {
{ SECRET_ERROR_IS_LOCKED, "org.freedesktop.Secret.Error.IsLocked", },
{ SECRET_ERROR_NO_SUCH_OBJECT, "org.freedesktop.Secret.Error.NoSuchObject", },
{ SECRET_ERROR_ALREADY_EXISTS, "org.freedesktop.Secret.Error.AlreadyExists" },
};
g_dbus_error_register_error_domain ("secret-error", &quark,
entries, G_N_ELEMENTS (entries));
return quark;
}
void
_secret_util_strip_remote_error (GError **error)
{
gchar *remote;
if (error == NULL || *error == NULL)
return;
remote = g_dbus_error_get_remote_error (*error);
if (remote) {
if (g_dbus_error_strip_remote_error (*error)) {
g_message ("Remote error from secret service: %s: %s",
remote, (*error)->message);
}
g_free (remote);
}
}
gchar *
_secret_util_parent_path (const gchar *path)
{
@ -177,10 +203,12 @@ on_get_properties (GObject *source,
retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
if (error == NULL)
if (error == NULL) {
process_get_all_reply (proxy, retval);
else
} else {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
}
if (retval != NULL)
g_variant_unref (retval);
@ -262,8 +290,10 @@ on_set_property (GObject *source,
retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
result, &error);
if (error != NULL)
if (error != NULL) {
_secret_util_strip_remote_error (&error);
g_simple_async_result_take_error (res, error);
}
if (retval != NULL)
g_variant_unref (retval);

View File

@ -378,6 +378,13 @@ test_service_path (Test *test,
egg_test_wait_idle ();
}
static void
null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data)
{
}
int
main (int argc, char **argv)
{
@ -385,6 +392,10 @@ main (int argc, char **argv)
g_set_prgname ("test-prompt");
g_type_init ();
/* Suppress these messages in tests */
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
null_log_handler, NULL);
g_test_add ("/prompt/run", Test, "mock-service-prompt.py", setup, test_perform_run, teardown);
g_test_add ("/prompt/perform-sync", Test, "mock-service-prompt.py", setup, test_perform_sync, teardown);
g_test_add ("/prompt/perform-async", Test, "mock-service-prompt.py", setup, test_perform_async, teardown);