[geocode-glib] lib: Check the number of results before returning



commit 87a3c4968d3ce64589c6b0440692cc3af5ea7dd8
Author: Bastien Nocera <hadess hadess net>
Date:   Thu May 5 12:48:00 2011 +0100

    lib: Check the number of results before returning
    
    When a request fails, we might not have an error, but an empty results
    set. Check for this, and error out properly instead of erroring
    because of a parsing error.

 geocode-glib/geocode-error.h |    4 +++-
 geocode-glib/geocode-glib.c  |   16 +++++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
---
diff --git a/geocode-glib/geocode-error.h b/geocode-glib/geocode-error.h
index 4ea1177..dfcb119 100644
--- a/geocode-glib/geocode-error.h
+++ b/geocode-glib/geocode-error.h
@@ -40,12 +40,14 @@ G_BEGIN_DECLS
  * GeocodeError:
  * @GEOCODE_ERROR_PARSE: An error occured parsing the response from the web service.
  * @GEOCODE_ERROR_NOT_SUPPORTED: The request made was not supported.
+ * @GEOCODE_ERROR_NO_MATCHES: The requests made didn't have any matches.
  *
  * Error codes returned by geocode-glib functions.
  **/
 typedef enum {
 	GEOCODE_ERROR_PARSE,
-	GEOCODE_ERROR_NOT_SUPPORTED
+	GEOCODE_ERROR_NOT_SUPPORTED,
+	GEOCODE_ERROR_NO_MATCHES
 } GeocodeError;
 
 
diff --git a/geocode-glib/geocode-glib.c b/geocode-glib/geocode-glib.c
index c91b5e2..5078861 100644
--- a/geocode-glib/geocode-glib.c
+++ b/geocode-glib/geocode-glib.c
@@ -217,7 +217,7 @@ _geocode_parse_json (const char *contents,
 	JsonParser *parser;
 	JsonNode *root;
 	JsonReader *reader;
-	gint64 err_code;
+	gint64 err_code, found;
 	guint i;
 	const GError *err = NULL;
 	char **members;
@@ -285,6 +285,20 @@ _geocode_parse_json (const char *contents,
 		return NULL;
 	}
 
+	/* Check for the number of results */
+	if (json_reader_read_member (reader, "Found") == FALSE)
+		goto parse;
+
+	found = json_reader_get_int_value (reader);
+	json_reader_end_member (reader);
+
+	if (!found) {
+		g_set_error_literal (error, GEOCODE_ERROR, GEOCODE_ERROR_NO_MATCHES, "No matches found for request");
+		g_object_unref (parser);
+		g_object_unref (reader);
+		return NULL;
+	}
+
 	if (json_reader_read_member (reader, "Results") == FALSE)
 		goto parse;
 



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