Skip to content

Commit ab88144

Browse files
committed
Handle IOExceptions went fetching remote settings
1 parent 5d56114 commit ab88144

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/settings/RemoteSettings.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import org.json.JSONException
3535
import org.json.JSONObject
3636

3737
internal class RemoteSettings(
38-
val context: Context,
39-
val blockingDispatcher: CoroutineContext,
40-
val backgroundDispatcher: CoroutineContext,
41-
val firebaseInstallationsApi: FirebaseInstallationsApi,
42-
val appInfo: ApplicationInfo,
38+
context: Context,
39+
private val blockingDispatcher: CoroutineContext,
40+
private val backgroundDispatcher: CoroutineContext,
41+
private val firebaseInstallationsApi: FirebaseInstallationsApi,
42+
private val appInfo: ApplicationInfo,
4343
private val configsFetcher: CrashlyticsSettingsFetcher = RemoteSettingsFetcher(appInfo),
4444
dataStoreName: String = SESSION_CONFIGS_NAME
4545
) : SettingsProvider {
@@ -80,7 +80,7 @@ internal class RemoteSettings(
8080
scope.launch { settingsCache.removeConfigs() }
8181
}
8282

83-
suspend private fun fetchConfigs() {
83+
private suspend fun fetchConfigs() {
8484
// Check if a fetch is in progress. If yes, return
8585
if (fetchInProgress.get()) {
8686
return
@@ -94,7 +94,7 @@ internal class RemoteSettings(
9494
fetchInProgress.set(true)
9595

9696
// Get the installations ID before making a remote config fetch
97-
var installationId = firebaseInstallationsApi.id.await()
97+
val installationId = firebaseInstallationsApi.id.await()
9898
if (installationId == null) {
9999
fetchInProgress.set(false)
100100
return
@@ -141,7 +141,6 @@ internal class RemoteSettings(
141141
}
142142
}
143143

144-
val scope = CoroutineScope(backgroundDispatcher)
145144
sessionsEnabled?.let { settingsCache.updateSettingsEnabled(sessionsEnabled) }
146145

147146
sessionTimeoutSeconds?.let {
@@ -156,9 +155,9 @@ internal class RemoteSettings(
156155
settingsCache.updateSessionCacheUpdatedTime(System.currentTimeMillis())
157156
fetchInProgress.set(false)
158157
},
159-
onFailure = {
158+
onFailure = { msg ->
160159
// Network request failed here.
161-
Log.e(TAG, "Error failing to fetch the remote configs")
160+
Log.e(TAG, "Error failing to fetch the remote configs: $msg")
162161
fetchInProgress.set(false)
163162
}
164163
)

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/settings/RemoteSettingsFetcher.kt

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.firebase.sessions.settings
1919
import android.net.Uri
2020
import com.google.firebase.sessions.ApplicationInfo
2121
import java.io.BufferedReader
22+
import java.io.IOException
2223
import java.io.InputStreamReader
2324
import java.net.URL
2425
import javax.net.ssl.HttpsURLConnection
@@ -27,43 +28,48 @@ import org.json.JSONObject
2728
internal interface CrashlyticsSettingsFetcher {
2829
suspend fun doConfigFetch(
2930
headerOptions: Map<String, String>,
30-
onSuccess: suspend ((JSONObject)) -> Unit,
31-
onFailure: suspend () -> Unit
31+
onSuccess: suspend (JSONObject) -> Unit,
32+
onFailure: suspend (msg: String) -> Unit
3233
)
3334
}
3435

35-
internal class RemoteSettingsFetcher(val appInfo: ApplicationInfo) : CrashlyticsSettingsFetcher {
36+
internal class RemoteSettingsFetcher(private val appInfo: ApplicationInfo) :
37+
CrashlyticsSettingsFetcher {
3638
override suspend fun doConfigFetch(
3739
headerOptions: Map<String, String>,
38-
onSuccess: suspend ((JSONObject)) -> Unit,
39-
onFailure: suspend () -> Unit
40+
onSuccess: suspend (JSONObject) -> Unit,
41+
onFailure: suspend (String) -> Unit
4042
) {
4143
val connection = settingsUrl().openConnection() as HttpsURLConnection
4244
connection.requestMethod = "GET"
4345
connection.setRequestProperty("Accept", "application/json")
4446
headerOptions.forEach { connection.setRequestProperty(it.key, it.value) }
4547

46-
val responseCode = connection.responseCode
47-
if (responseCode == HttpsURLConnection.HTTP_OK) {
48-
val inputStream = connection.inputStream
49-
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
50-
val response = StringBuilder()
51-
var inputLine: String?
52-
while (bufferedReader.readLine().also { inputLine = it } != null) {
53-
response.append(inputLine)
54-
}
55-
bufferedReader.close()
56-
inputStream.close()
48+
try {
49+
val responseCode = connection.responseCode
50+
if (responseCode == HttpsURLConnection.HTTP_OK) {
51+
val inputStream = connection.inputStream
52+
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
53+
val response = StringBuilder()
54+
var inputLine: String?
55+
while (bufferedReader.readLine().also { inputLine = it } != null) {
56+
response.append(inputLine)
57+
}
58+
bufferedReader.close()
59+
inputStream.close()
5760

58-
val responseJson = JSONObject(response.toString())
59-
onSuccess(responseJson)
60-
} else {
61-
onFailure()
61+
val responseJson = JSONObject(response.toString())
62+
onSuccess(responseJson)
63+
} else {
64+
onFailure("Bad response code: $responseCode")
65+
}
66+
} catch (ex: IOException) {
67+
onFailure(ex.message ?: ex.toString())
6268
}
6369
}
6470

65-
fun settingsUrl(): URL {
66-
var uri =
71+
private fun settingsUrl(): URL {
72+
val uri =
6773
Uri.Builder()
6874
.scheme("https")
6975
.authority(FIREBASE_SESSIONS_BASE_URL_STRING)

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/testing/FakeRemoteConfigFetcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class FakeRemoteConfigFetcher(var responseJSONObject: JSONObject = JSON
2424
override suspend fun doConfigFetch(
2525
headerOptions: Map<String, String>,
2626
onSuccess: suspend (JSONObject) -> Unit,
27-
onFailure: suspend () -> Unit
27+
onFailure: suspend (String) -> Unit
2828
) {
2929
onSuccess(responseJSONObject)
3030
}

0 commit comments

Comments
 (0)