Skip to content
Permalink
Browse files

Issue #565: Fretboard: Handle JSON exceptions on Kinto

Closes #565: Fretboard: Handle JSON exceptions on Kinto
  • Loading branch information...
fercarcedo authored and pocmo committed Aug 3, 2018
1 parent 170b874 commit 0ba81f51500a063a71b8db35540fb9aeab15218b
@@ -4,10 +4,11 @@

package mozilla.components.service.fretboard.source.kinto

import mozilla.components.service.fretboard.ExperimentDownloadException
import mozilla.components.service.fretboard.ExperimentSource
import mozilla.components.service.fretboard.JSONExperimentParser
import mozilla.components.service.fretboard.ExperimentsSnapshot
import org.json.JSONArray
import mozilla.components.service.fretboard.JSONExperimentParser
import org.json.JSONException
import org.json.JSONObject

/**
@@ -41,25 +42,28 @@ class KintoExperimentSource(

private fun mergeExperimentsFromDiff(experimentsDiff: String, snapshot: ExperimentsSnapshot): ExperimentsSnapshot {
val experiments = snapshot.experiments
var maxLastModified: Long? = snapshot.lastModified
val mutableExperiments = experiments.toMutableList()
val experimentParser = JSONExperimentParser()
val diffJsonObject = JSONObject(experimentsDiff)
val data = diffJsonObject.get(DATA_KEY)
val experimentsJsonArray = data as JSONArray
var maxLastModified: Long? = snapshot.lastModified
for (i in 0 until experimentsJsonArray.length()) {
val experimentJsonObject = experimentsJsonArray[i] as JSONObject
val experiment = mutableExperiments.singleOrNull { it.id == experimentJsonObject.getString(ID_KEY) }
if (experiment != null) {
mutableExperiments.remove(experiment)
}
if (!experimentJsonObject.has(DELETED_KEY)) {
mutableExperiments.add(experimentParser.fromJson(experimentJsonObject))
}
val lastModifiedDate = experimentJsonObject.getLong(LAST_MODIFIED_KEY)
if (maxLastModified == null || lastModifiedDate > maxLastModified) {
maxLastModified = lastModifiedDate
try {
val diffJsonObject = JSONObject(experimentsDiff)
val experimentsJsonArray = diffJsonObject.getJSONArray(DATA_KEY)
for (i in 0 until experimentsJsonArray.length()) {
val experimentJsonObject = experimentsJsonArray.getJSONObject(i)
val experiment = mutableExperiments.singleOrNull { it.id == experimentJsonObject.getString(ID_KEY) }
if (experiment != null) {
mutableExperiments.remove(experiment)
}
if (!experimentJsonObject.has(DELETED_KEY)) {
mutableExperiments.add(experimentParser.fromJson(experimentJsonObject))
}
val lastModifiedDate = experimentJsonObject.getLong(LAST_MODIFIED_KEY)
if (maxLastModified == null || lastModifiedDate > maxLastModified) {
maxLastModified = lastModifiedDate
}
}
} catch (e: JSONException) {
throw ExperimentDownloadException(e.message)
}
return ExperimentsSnapshot(mutableExperiments, maxLastModified)
}

0 comments on commit 0ba81f5

Please sign in to comment.
You can’t perform that action at this time.