[tracker/dbus-fd-experiment] libtracker-client: Add proper error handling to Steroids



commit 083b3f23fb99d489ea48d0c6ec3f7a91e162757d
Author: Adrien Bustany <abustany gnome org>
Date:   Mon May 24 10:09:03 2010 -0400

    libtracker-client: Add proper error handling to Steroids

 src/libtracker-client/tracker.c |  145 ++++++++++++++++++++++++++++++++++++---
 src/libtracker-client/tracker.h |    3 +-
 2 files changed, 136 insertions(+), 12 deletions(-)
---
diff --git a/src/libtracker-client/tracker.c b/src/libtracker-client/tracker.c
index 8c76905..7fc416c 100644
--- a/src/libtracker-client/tracker.c
+++ b/src/libtracker-client/tracker.c
@@ -725,6 +725,124 @@ pipe_read (int fd, char *dst, int size)
 	}
 }
 
+/* Copied from dbus-gobject.c, in DBus-GLib */
+static gint
+dbus_error_to_gerror_code (const char *derr)
+{
+	if (0) ;
+	else if (!strcmp (derr, DBUS_ERROR_FAILED))
+		return DBUS_GERROR_FAILED ;
+	else if (!strcmp (derr, DBUS_ERROR_NO_MEMORY))
+		return DBUS_GERROR_NO_MEMORY ;
+	else if (!strcmp (derr, DBUS_ERROR_SERVICE_UNKNOWN))
+		return DBUS_GERROR_SERVICE_UNKNOWN ;
+	else if (!strcmp (derr, DBUS_ERROR_NAME_HAS_NO_OWNER))
+		return DBUS_GERROR_NAME_HAS_NO_OWNER ;
+	else if (!strcmp (derr, DBUS_ERROR_NO_REPLY))
+		return DBUS_GERROR_NO_REPLY ;
+	else if (!strcmp (derr, DBUS_ERROR_IO_ERROR))
+		return DBUS_GERROR_IO_ERROR ;
+	else if (!strcmp (derr, DBUS_ERROR_BAD_ADDRESS))
+		return DBUS_GERROR_BAD_ADDRESS ;
+	else if (!strcmp (derr, DBUS_ERROR_NOT_SUPPORTED))
+		return DBUS_GERROR_NOT_SUPPORTED ;
+	else if (!strcmp (derr, DBUS_ERROR_LIMITS_EXCEEDED))
+		return DBUS_GERROR_LIMITS_EXCEEDED ;
+	else if (!strcmp (derr, DBUS_ERROR_ACCESS_DENIED))
+		return DBUS_GERROR_ACCESS_DENIED ;
+	else if (!strcmp (derr, DBUS_ERROR_AUTH_FAILED))
+		return DBUS_GERROR_AUTH_FAILED ;
+	else if (!strcmp (derr, DBUS_ERROR_NO_SERVER))
+		return DBUS_GERROR_NO_SERVER ;
+	else if (!strcmp (derr, DBUS_ERROR_TIMEOUT))
+		return DBUS_GERROR_TIMEOUT ;
+	else if (!strcmp (derr, DBUS_ERROR_NO_NETWORK))
+		return DBUS_GERROR_NO_NETWORK ;
+	else if (!strcmp (derr, DBUS_ERROR_ADDRESS_IN_USE))
+		return DBUS_GERROR_ADDRESS_IN_USE ;
+	else if (!strcmp (derr, DBUS_ERROR_DISCONNECTED))
+		return DBUS_GERROR_DISCONNECTED ;
+	else if (!strcmp (derr, DBUS_ERROR_INVALID_ARGS))
+		return DBUS_GERROR_INVALID_ARGS ;
+	else if (!strcmp (derr, DBUS_ERROR_FILE_NOT_FOUND))
+		return DBUS_GERROR_FILE_NOT_FOUND ;
+	else if (!strcmp (derr, DBUS_ERROR_FILE_EXISTS))
+		return DBUS_GERROR_FILE_EXISTS ;
+	else if (!strcmp (derr, DBUS_ERROR_UNKNOWN_METHOD))
+		return DBUS_GERROR_UNKNOWN_METHOD ;
+	else if (!strcmp (derr, DBUS_ERROR_TIMED_OUT))
+		return DBUS_GERROR_TIMED_OUT ;
+	else if (!strcmp (derr, DBUS_ERROR_MATCH_RULE_NOT_FOUND))
+		return DBUS_GERROR_MATCH_RULE_NOT_FOUND ;
+	else if (!strcmp (derr, DBUS_ERROR_MATCH_RULE_INVALID))
+		return DBUS_GERROR_MATCH_RULE_INVALID ;
+	else if (!strcmp (derr, DBUS_ERROR_SPAWN_EXEC_FAILED))
+		return DBUS_GERROR_SPAWN_EXEC_FAILED ;
+	else if (!strcmp (derr, DBUS_ERROR_SPAWN_FORK_FAILED))
+		return DBUS_GERROR_SPAWN_FORK_FAILED ;
+	else if (!strcmp (derr, DBUS_ERROR_SPAWN_CHILD_EXITED))
+		return DBUS_GERROR_SPAWN_CHILD_EXITED ;
+	else if (!strcmp (derr, DBUS_ERROR_SPAWN_CHILD_SIGNALED))
+		return DBUS_GERROR_SPAWN_CHILD_SIGNALED ;
+	else if (!strcmp (derr, DBUS_ERROR_SPAWN_FAILED))
+		return DBUS_GERROR_SPAWN_FAILED ;
+	else if (!strcmp (derr, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN))
+		return DBUS_GERROR_UNIX_PROCESS_ID_UNKNOWN ;
+	else if (!strcmp (derr, DBUS_ERROR_INVALID_SIGNATURE))
+		return DBUS_GERROR_INVALID_SIGNATURE ;
+	else if (!strcmp (derr, DBUS_ERROR_INVALID_FILE_CONTENT))
+		return DBUS_GERROR_INVALID_FILE_CONTENT ;
+	else if (!strcmp (derr, DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN))
+		return DBUS_GERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN ;
+	else
+		return DBUS_GERROR_REMOTE_EXCEPTION;
+}
+
+/**
+ * dbus_set_g_error:
+ * @gerror: an error
+ * @error: a #DBusError
+ *
+ * Store the information from a DBus method error return into a
+ * GError.  For the normal case of an arbitrary remote process,
+ * the error code will be DBUS_GERROR_REMOTE_EXCEPTION.  Now,
+ * DBus errors have two components; a message and a "name".
+ * The former is an arbitrary (normally American English) string.
+ * The second is a string like com.example.FooFailure which
+ * programs can use as a conditional source.  Because a GError
+ * only has one string, we use a hack to encode both values:
+ *
+ * &lt;human readable string&gt;&lt;null&gt;&lt;error name&gt;&lt;null&gt;
+ *
+ * You can use the following code to retrieve both values:
+ *
+ * |[const char *msg = error->message;
+ * size_t len = strlen(msg);
+ * const char *error_name = msg+len+1;]|
+ *
+ * Copied from dbus-gobject.c, DBus-GLib
+ */
+void
+dbus_set_g_error (GError    **gerror,
+                  DBusError  *error)
+{
+	int code;
+
+	code = dbus_error_to_gerror_code (error->name);
+	if (code != DBUS_GERROR_REMOTE_EXCEPTION)
+		g_set_error (gerror, DBUS_GERROR,
+		             code,
+		             "%s",
+		             error->message);
+	else
+		g_set_error (gerror, DBUS_GERROR,
+		             code,
+		             "%s%c%s",
+		             error->message ? error->message : "",
+		             '\0',
+		             error->name);
+}
+
 /**
  * tracker_uri_vprintf_escaped:
  * @format: a standard printf() format string, but notice
@@ -1129,7 +1247,7 @@ tracker_resources_sparql_query_steroids (TrackerClient  *client,
 	dbus_message_unref (message);
 
 	if (!reply) {
-		g_critical ("DBus error in steroids: %s %s", dbus_error.name, dbus_error.message);
+		dbus_set_g_error (error, &dbus_error);
 		return NULL;
 	}
 
@@ -1149,10 +1267,19 @@ tracker_resources_sparql_query_steroids (TrackerClient  *client,
 	dbus_message_append_args (message,
 	                          DBUS_TYPE_UINT32, &query_id,
 	                          DBUS_TYPE_INVALID);
-	dbus_connection_send_with_reply (connection,
-	                                 message,
-	                                 &iterator->call,
-	                                 INT_MAX);
+	reply = dbus_connection_send_with_reply_and_block (connection,
+	                                                   message,
+	                                                   -1,
+	                                                   &dbus_error);
+	dbus_message_unref (message);
+
+	if (!reply) {
+		dbus_set_g_error (error, &dbus_error);
+		tracker_result_iterator_free (iterator);
+		return NULL;
+	}
+
+	dbus_message_unref (reply);
 
 	/* Pre fetch the first buffer to initialize the iterator */
 	pipe_read (iterator->fd, iterator->buffer, STEROIDS_BUFFER_SIZE);
@@ -1167,7 +1294,6 @@ tracker_resources_sparql_query_steroids (TrackerClient  *client,
 	case STEROIDS_RC_DONE:
 		break;
 	case STEROIDS_RC_ERROR:
-		//TODO: Set error
 		tracker_result_iterator_free (iterator);
 		iterator = NULL;
 		break;
@@ -1202,8 +1328,7 @@ tracker_result_iterator_has_next (TrackerResultIterator *iterator)
 }
 
 void
-tracker_result_iterator_next (TrackerResultIterator  *iterator,
-                              GError                **error)
+tracker_result_iterator_next (TrackerResultIterator  *iterator)
 {
 	int row_size;
 
@@ -1249,8 +1374,8 @@ tracker_result_iterator_next (TrackerResultIterator  *iterator,
 		case STEROIDS_RC_DONE:
 			break;
 		default:
-			/* Other type of error */
-			// TODO: Set error
+			/* If an error happened, it has been reported by
+			 * tracker_resources_sparql_query_steroids */
 			break;
 	}
 }
diff --git a/src/libtracker-client/tracker.h b/src/libtracker-client/tracker.h
index 34ddcc9..34d3153 100644
--- a/src/libtracker-client/tracker.h
+++ b/src/libtracker-client/tracker.h
@@ -131,8 +131,7 @@ TrackerResultIterator *
 void           tracker_result_iterator_free                (TrackerResultIterator  *iterator);
 guint          tracker_result_iterator_n_columns           (TrackerResultIterator  *iterator);
 gboolean       tracker_result_iterator_has_next            (TrackerResultIterator  *iterator);
-void           tracker_result_iterator_next                (TrackerResultIterator  *iterator,
-                                                            GError                **error);
+void           tracker_result_iterator_next                (TrackerResultIterator  *iterator);
 const gchar *  tracker_result_iterator_value               (TrackerResultIterator  *iterator,
                                                             guint                   column);
 void           tracker_resources_sparql_update             (TrackerClient          *client,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]