@@ -9,6 +9,7 @@ import com.ninecraft.booket.core.datastore.impl.util.handleIOException
99import com.orhanobut.logger.Logger
1010import kotlinx.coroutines.flow.Flow
1111import kotlinx.coroutines.flow.map
12+ import kotlinx.serialization.SerializationException
1213import kotlinx.serialization.json.Json
1314import javax.inject.Inject
1415
@@ -22,8 +23,11 @@ class DefaultRecentSearchDataSource @Inject constructor(
2223 prefs[RECENT_SEARCHES ]?.let { jsonString ->
2324 try {
2425 Json .decodeFromString<List <String >>(jsonString)
26+ } catch (e: SerializationException ) {
27+ Logger .e(e, " Failed to deserialize recent searches" )
28+ emptyList()
2529 } catch (e: Exception ) {
26- Logger .e(e.toString() )
30+ Logger .e(e, " Unexpected error while reading recent searches " )
2731 emptyList()
2832 }
2933 } ? : emptyList()
@@ -37,8 +41,11 @@ class DefaultRecentSearchDataSource @Inject constructor(
3741 val currentSearches = prefs[RECENT_SEARCHES ]?.let { jsonString ->
3842 try {
3943 Json .decodeFromString<List <String >>(jsonString).toMutableList()
44+ } catch (e: SerializationException ) {
45+ Logger .e(e, " Failed to deserialize recent searches for adding" )
46+ mutableListOf ()
4047 } catch (e: Exception ) {
41- Logger .e(e.toString() )
48+ Logger .e(e, " Unexpected error while adding recent search " )
4249 mutableListOf ()
4350 }
4451 } ? : mutableListOf ()
@@ -50,7 +57,11 @@ class DefaultRecentSearchDataSource @Inject constructor(
5057
5158 // 최근 10개만 유지
5259 val limitedSearches = currentSearches.take(MAX_SEARCH_COUNT )
53- prefs[RECENT_SEARCHES ] = Json .encodeToString(limitedSearches)
60+ try {
61+ prefs[RECENT_SEARCHES ] = Json .encodeToString(limitedSearches)
62+ } catch (e: SerializationException ) {
63+ Logger .e(e, " Failed to serialize recent searches" )
64+ }
5465 }
5566 }
5667
@@ -60,14 +71,21 @@ class DefaultRecentSearchDataSource @Inject constructor(
6071 val currentSearches = prefs[RECENT_SEARCHES ]?.let { jsonString ->
6172 try {
6273 Json .decodeFromString<List <String >>(jsonString).toMutableList()
74+ } catch (e: SerializationException ) {
75+ Logger .e(e, " Failed to deserialize recent searches for adding" )
76+ mutableListOf ()
6377 } catch (e: Exception ) {
64- Logger .e(e.toString() )
78+ Logger .e(e, " Unexpected error while adding recent search " )
6579 mutableListOf ()
6680 }
6781 } ? : mutableListOf ()
6882
6983 currentSearches.remove(query)
70- prefs[RECENT_SEARCHES ] = Json .encodeToString(currentSearches)
84+ try {
85+ prefs[RECENT_SEARCHES ] = Json .encodeToString(currentSearches)
86+ } catch (e: SerializationException ) {
87+ Logger .e(e, " Failed to serialize recent searches after removal" )
88+ }
7189 }
7290 }
7391
0 commit comments