Skip to content

Commit

Permalink
feat: handle no internet exception
Browse files Browse the repository at this point in the history
  • Loading branch information
emiriko committed Jun 20, 2024
1 parent 6df96ad commit 6c3d64f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.ulascan.app.data.states.ResultState
import com.ulascan.app.utils.getErrorMessage
import kotlinx.coroutines.flow.Flow
import retrofit2.HttpException
import java.io.IOException

class UserRepository(
private val userPreferences: UserPreferences,
Expand All @@ -33,6 +34,10 @@ class UserRepository(
} catch (error: HttpException) {
userPreferences.clearToken()
ResultState.Error(error.getErrorMessage())
} catch (error: IOException) {
ResultState.Error("No internet connection. Please check your network and try again.")
} catch (error: Exception) {
ResultState.Error("An unexpected error occurred: ${error.localizedMessage}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.ulascan.app.data.states.ResultState
import com.ulascan.app.utils.getErrorMessage
import kotlinx.coroutines.flow.Flow
import retrofit2.HttpException
import java.io.IOException

class AuthenticatedChatRepository(private val apiService: ApiService) : UserChatRepository {
override suspend fun getHistory(): ResultState<HistoriesResponse> {
Expand All @@ -20,6 +21,10 @@ class AuthenticatedChatRepository(private val apiService: ApiService) : UserChat
ResultState.Success(response)
} catch (error: HttpException) {
ResultState.Error(error.getErrorMessage())
} catch (error: IOException) {
ResultState.Error("No internet connection. Please check your network and try again.")
} catch (error: Exception) {
ResultState.Error("An unexpected error occurred: ${error.localizedMessage}")
}
}

Expand All @@ -36,6 +41,10 @@ class AuthenticatedChatRepository(private val apiService: ApiService) : UserChat
ResultState.Success(response)
} catch (error: HttpException) {
ResultState.Error(error.getErrorMessage())
} catch (error: IOException) {
ResultState.Error("No internet connection. Please check your network and try again.")
} catch (error: Exception) {
ResultState.Error("An unexpected error occurred: ${error.localizedMessage}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ulascan.app.data.repository.ChatRepository
import com.ulascan.app.data.states.ResultState
import com.ulascan.app.utils.getErrorMessage
import retrofit2.HttpException
import java.io.IOException

class GuestChatRepository(private val apiService: ApiService) : ChatRepository {

Expand All @@ -15,6 +16,10 @@ class GuestChatRepository(private val apiService: ApiService) : ChatRepository {
ResultState.Success(response)
} catch (error: HttpException) {
ResultState.Error(error.getErrorMessage())
} catch (error: IOException) {
ResultState.Error("No internet connection. Please check your network and try again.")
} catch (error: Exception) {
ResultState.Error("An unexpected error occurred: ${error.localizedMessage}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException

class LoginViewModel(private val userRepository: UserRepository) : ViewModel() {

Expand All @@ -31,6 +32,9 @@ class LoginViewModel(private val userRepository: UserRepository) : ViewModel() {
}
} catch (e: HttpException) {
_uiState.value = LoginUiState.Error(e.getErrorMessage())
} catch (e: IOException) {
_uiState.value =
LoginUiState.Error("No internet connection. Please check your network and try again.")
} catch (e: Exception) {
_uiState.value = LoginUiState.Error(e.message ?: "Unknown error")
}
Expand Down

0 comments on commit 6c3d64f

Please sign in to comment.