Skip to content

Commit

Permalink
🐛 Fix error handling in flow
Browse files Browse the repository at this point in the history
  • Loading branch information
paul2126 committed Dec 10, 2023
1 parent 4f4f1e4 commit fb955e4
Showing 1 changed file with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.example.speechbuddy.data.remote.requests.BackupWeightTableRequest
import com.example.speechbuddy.service.BackupService
import com.example.speechbuddy.utils.ResponseHandler
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import retrofit2.Response
import javax.inject.Inject
Expand All @@ -17,41 +18,34 @@ class SettingsRemoteSource @Inject constructor(
) {
suspend fun getDisplaySettings(authHeader: String): Flow<Response<SettingsBackupDto>> =
flow {
try {
val result = backupService.getDisplaySettings(authHeader)
emit(result)
} catch (e: Exception) {
emit(responseHandler.getConnectionErrorResponse())
}
val result = backupService.getDisplaySettings(authHeader)
emit(result)
}.catch {
emit(responseHandler.getConnectionErrorResponse())
}

suspend fun getSymbolList(authHeader: String): Flow<Response<SymbolListDto>> =
flow {
try {
val result = backupService.getSymbolList(authHeader)
emit(result)
} catch (e: Exception) {
emit(responseHandler.getConnectionErrorResponse())
}
val result = backupService.getSymbolList(authHeader)
emit(result)
}.catch {
emit(responseHandler.getConnectionErrorResponse())
}

suspend fun getFavoritesList(authHeader: String): Flow<Response<FavoritesListDto>> =
flow {
try {
val result = backupService.getFavoriteSymbolList(authHeader)
emit(result)
} catch (e: Exception) {
emit(responseHandler.getConnectionErrorResponse())
}
val result = backupService.getFavoriteSymbolList(authHeader)
emit(result)
}.catch {
emit(responseHandler.getConnectionErrorResponse())
}


suspend fun getWeightTable(authHeader: String): Flow<Response<BackupWeightTableRequest>> =
flow {
try {
val result = backupService.getWeightTable(authHeader)
emit(result)
} catch (e: Exception) {
emit(responseHandler.getConnectionErrorResponse())
}
val result = backupService.getWeightTable(authHeader)
emit(result)
}.catch {
emit(responseHandler.getConnectionErrorResponse())
}
}

0 comments on commit fb955e4

Please sign in to comment.