Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Catch JSONException from parsing vardata response #33

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlin.jvm.Throws
import org.json.JSONArray
import org.json.JSONException

internal class DefaultExperimentClient internal constructor(
private val apiKey: String,
Expand Down Expand Up @@ -263,13 +264,17 @@ internal class DefaultExperimentClient internal constructor(
throw IOException("fetch error response: $response")
}
val body = response.body?.string() ?: ""
val json = JSONObject(body)
val variants = mutableMapOf<String, Variant>()
json.keys().forEach { key ->
val variant = json.getJSONObject(key).toVariant()
if (variant != null) {
variants[key] = variant
try {
val json = JSONObject(body)
json.keys().forEach { key ->
val variant = json.getJSONObject(key).toVariant()
if (variant != null) {
variants[key] = variant
}
}
} catch (e: JSONException) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should catch this exception from the place where parseResponse is called rather than inside this function. Line 238. Then we can call the onFailure function to complete the future exceptionally and propagate the error more clearly.

Logger.w("Error converting fetch variants response to JSONObject", e)
}
return variants
}
Expand Down
Loading