Skip to content

Commit

Permalink
"You don't have permission to edit groups" after sync network failure.
Browse files Browse the repository at this point in the history
…Closes #103
  • Loading branch information
Dima-Android committed Jan 4, 2024
1 parent 7ad56fe commit 32f5f30
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import okhttp3.Headers
sealed class CustomResult<out T> {
sealed class GeneralError : CustomResult<Nothing>() {
data class NetworkError(val httpCode: Int, val stringResponse: String?) : GeneralError() {
companion object {
const val NO_INTERNET_CONNECTION_HTTP_CODE = -1
const val UNKNOWN_NETWORK_EXCEPTION_HTTP_CODE = -999
}


fun isUnchanged(): Boolean {
return httpCode == 304
}
fun isNoNetworkError(): Boolean {
return httpCode == NO_INTERNET_CONNECTION_HTTP_CODE
}
}

data class CodeError(val throwable: Throwable) : GeneralError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.zotero.android.api.network
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.Response
import java.net.SocketTimeoutException
import java.net.UnknownHostException

object NetworkHelper {
fun <T> parseNetworkResponse(networkResponse: Response<T>): CustomResult<T> {
Expand All @@ -20,6 +22,18 @@ object NetworkHelper {
stringResponse = errorBody?.string()
)
}
fun <T> parseNetworkException(e: Exception): CustomResult<T> {
val isNoNetworkError = e is UnknownHostException || e is SocketTimeoutException
return CustomResult.GeneralError.NetworkError(
httpCode = if (isNoNetworkError) {
CustomResult.GeneralError.NetworkError.NO_INTERNET_CONNECTION_HTTP_CODE
} else {
CustomResult.GeneralError.NetworkError.UNKNOWN_NETWORK_EXCEPTION_HTTP_CODE
},
stringResponse = e.localizedMessage
)
}


}

Expand All @@ -30,7 +44,7 @@ suspend fun <T> safeApiCall(apiCall: suspend () -> Response<T>): CustomResult<T>
val parseNetworkResponse = NetworkHelper.parseNetworkResponse(networkResponse)
parseNetworkResponse
} catch (e: Exception) {
CustomResult.GeneralError.CodeError(e)
NetworkHelper.parseNetworkException(e)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ internal class LoginViewModel @Inject constructor(
copy(isLoading = false)
}
val error = networkResult as CustomResult.GeneralError.NetworkError
if (error.httpCode == 403) {
if (error.isNoNetworkError()) {
showErrorRes(Strings.errors_sync_toolbar_internet_connection)
} else if (error.httpCode == 403) {
showErrorRes(Strings.errors_login_invalid_credentials)
} else {
showError(error.stringResponse)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/zotero/android/sync/SyncEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,9 @@ class SyncUseCase @Inject constructor(
response: String,
data: SyncError.ErrorData
): SyncError {
if (error.isNoNetworkError()) {
return SyncError.fatal2(SyncError.Fatal.noInternetConnection)
}
val responseMessage: () -> String = {
if (response == "No Response") {
error.stringResponse ?: "No error to parse"
Expand Down

0 comments on commit 32f5f30

Please sign in to comment.