From 9e36bdc5a6a749291d36bf29d38eb0bc338786b4 Mon Sep 17 00:00:00 2001 From: Shuvo Date: Mon, 25 Nov 2024 20:34:35 +0600 Subject: [PATCH] Catch exception and replace with default data --- .../core/datastore/NiaPreferencesDataSource.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt b/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt index 5cc237ff7c..ba45606dfb 100644 --- a/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt +++ b/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt @@ -164,6 +164,14 @@ class NiaPreferencesDataSource @Inject constructor( } suspend fun getChangeListVersions() = userPreferences.data + .catch { exception -> + if (exception is IOException) { + Log.e("NiaPreferences", "Error reading user preferences.", exception) + emit(UserPreferences.getDefaultInstance()) + } else { + throw exception + } + } .map { ChangeListVersions( topicVersion = it.topicChangeListVersion,