Merge pull request #3900 from vector-im/feature/fre/expired_account_error

Add expired account error code
This commit is contained in:
Benoit Marty 2021-08-27 15:09:08 +02:00 committed by GitHub
commit e78434d25c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 6 deletions

1
changelog.d/3900.feature Normal file
View file

@ -0,0 +1 @@
Add expired account error code in the matrix SDK

View file

@ -29,7 +29,9 @@ fun Throwable.is401() =
fun Throwable.isTokenError() = fun Throwable.isTokenError() =
this is Failure.ServerError this is Failure.ServerError
&& (error.code == MatrixError.M_UNKNOWN_TOKEN || error.code == MatrixError.M_MISSING_TOKEN) && (error.code == MatrixError.M_UNKNOWN_TOKEN
|| error.code == MatrixError.M_MISSING_TOKEN
|| error.code == MatrixError.ORG_MATRIX_EXPIRED_ACCOUNT)
fun Throwable.shouldBeRetried(): Boolean { fun Throwable.shouldBeRetried(): Boolean {
return this is Failure.NetworkConnection return this is Failure.NetworkConnection

View file

@ -23,4 +23,5 @@ sealed class GlobalError {
data class InvalidToken(val softLogout: Boolean) : GlobalError() data class InvalidToken(val softLogout: Boolean) : GlobalError()
data class ConsentNotGivenError(val consentUri: String) : GlobalError() data class ConsentNotGivenError(val consentUri: String) : GlobalError()
data class CertificateError(val fingerprint: Fingerprint) : GlobalError() data class CertificateError(val fingerprint: Fingerprint) : GlobalError()
object ExpiredAccount : GlobalError()
} }

View file

@ -189,5 +189,12 @@ data class MatrixError(
// Possible value for "limit_type" // Possible value for "limit_type"
const val LIMIT_TYPE_MAU = "monthly_active_user" const val LIMIT_TYPE_MAU = "monthly_active_user"
/**
* The user account has expired. It has to be renewed by clicking on an email or by sending a renewal token.
*
* More documentation can be found in the dedicated Synapse plugin module repository: https://github.com/matrix-org/synapse-email-account-validity
*/
const val ORG_MATRIX_EXPIRED_ACCOUNT = "ORG_MATRIX_EXPIRED_ACCOUNT"
} }
} }

View file

@ -19,13 +19,13 @@
package org.matrix.android.sdk.internal.network package org.matrix.android.sdk.internal.network
import com.squareup.moshi.JsonEncodingException import com.squareup.moshi.JsonEncodingException
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.ResponseBody
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.GlobalError import org.matrix.android.sdk.api.failure.GlobalError
import org.matrix.android.sdk.api.failure.MatrixError import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.MoshiProvider
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.ResponseBody
import org.matrix.android.sdk.api.extensions.orFalse
import retrofit2.HttpException import retrofit2.HttpException
import retrofit2.Response import retrofit2.Response
import timber.log.Timber import timber.log.Timber
@ -86,16 +86,18 @@ private fun toFailure(errorBody: ResponseBody?, httpCode: Int, globalErrorReceiv
val matrixError = matrixErrorAdapter.fromJson(errorBodyStr) val matrixError = matrixErrorAdapter.fromJson(errorBodyStr)
if (matrixError != null) { if (matrixError != null) {
// Also send following errors to the globalErrorReceiver, for a global management
when { when {
matrixError.code == MatrixError.M_CONSENT_NOT_GIVEN && !matrixError.consentUri.isNullOrBlank() -> { matrixError.code == MatrixError.M_CONSENT_NOT_GIVEN && !matrixError.consentUri.isNullOrBlank() -> {
// Also send this error to the globalErrorReceiver, for a global management
globalErrorReceiver?.handleGlobalError(GlobalError.ConsentNotGivenError(matrixError.consentUri)) globalErrorReceiver?.handleGlobalError(GlobalError.ConsentNotGivenError(matrixError.consentUri))
} }
httpCode == HttpURLConnection.HTTP_UNAUTHORIZED /* 401 */ httpCode == HttpURLConnection.HTTP_UNAUTHORIZED /* 401 */
&& matrixError.code == MatrixError.M_UNKNOWN_TOKEN -> { && matrixError.code == MatrixError.M_UNKNOWN_TOKEN -> {
// Also send this error to the globalErrorReceiver, for a global management
globalErrorReceiver?.handleGlobalError(GlobalError.InvalidToken(matrixError.isSoftLogout.orFalse())) globalErrorReceiver?.handleGlobalError(GlobalError.InvalidToken(matrixError.isSoftLogout.orFalse()))
} }
matrixError.code == MatrixError.ORG_MATRIX_EXPIRED_ACCOUNT -> {
globalErrorReceiver?.handleGlobalError(GlobalError.ExpiredAccount)
}
} }
return Failure.ServerError(matrixError, httpCode) return Failure.ServerError(matrixError, httpCode)

View file

@ -267,6 +267,7 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), HasSc
activeSessionHolder.getActiveSession().sessionParams.homeServerHost ?: "") activeSessionHolder.getActiveSession().sessionParams.homeServerHost ?: "")
is GlobalError.CertificateError -> is GlobalError.CertificateError ->
handleCertificateError(globalError) handleCertificateError(globalError)
GlobalError.ExpiredAccount -> Unit // TODO Handle account expiration
}.exhaustive }.exhaustive
} }