[chronojump] Simplifies code when error checking and avoids a possible crash.
- From: Carles Pina i Estany <carlespina src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] Simplifies code when error checking and avoids a possible crash.
- Date: Tue, 25 Oct 2016 22:05:01 +0000 (UTC)
commit 7959883dc32ba290134e316327eb244d097952f5
Author: Carles Pina i Estany <carles pina cat>
Date: Wed Oct 26 00:04:03 2016 +0200
Simplifies code when error checking and avoids a possible crash.
(the crash would have happened if the chronojump_importer.py didn't
return the databaseVersion)
src/chronojumpImporter.cs | 11 +++++++----
src/json.cs | 15 +++++++++++++++
2 files changed, 22 insertions(+), 4 deletions(-)
---
diff --git a/src/chronojumpImporter.cs b/src/chronojumpImporter.cs
index 8bd4683..7cad699 100644
--- a/src/chronojumpImporter.cs
+++ b/src/chronojumpImporter.cs
@@ -234,11 +234,14 @@ class ChronojumpImporter
} else {
JsonValue json = JsonValue.Parse (information.output);
+ if (!json.ContainsKey ("sessions")) {
+ LogB.Information ("Trying to import a session but sessions doesn't exist.
Output:" + information.output);
+ return "UNKNOWN";
+ }
+
foreach(JsonValue session in json["sessions"])
{
- if (session ["uniqueID"] == sessionId) {
- return session ["name"];
- }
+ return JsonUtils.valueOrDefault (session, "name", "UNKNOWN");
}
LogB.Information ("Trying to import a session that we can't find the name. Output:" +
information.output);
return "UNKNOWN";
@@ -251,7 +254,7 @@ class ChronojumpImporter
if (information.success) {
JsonValue json = JsonValue.Parse (information.output);
- return new Result (true, json ["databaseVersion"]);
+ return new Result (true, JsonUtils.valueOrDefault(json, "databaseVersion", "0"));
} else {
return information;
}
diff --git a/src/json.cs b/src/json.cs
index bb930ce..4814f26 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -244,3 +244,18 @@ public class Json
~Json() {}
}
+
+class JsonUtils
+{
+ public static JsonValue valueOrDefault(JsonValue jsonObject, string key, string defaultValue)
+ {
+ // Returns jsonObject[key] if it exists. If the key doesn't exist returns defaultValue and
+ // logs the anomaly into the Chronojump log.
+ if (jsonObject.ContainsKey (key)) {
+ return jsonObject ["key"];
+ } else {
+ LogB.Information ("JsonUtils::valueOrDefault: returning default (" + defaultValue +
") from JSON: " + jsonObject.ToString ());
+ return defaultValue;
+ }
+ }
+}
\ No newline at end of file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]