extracting error cases to extensions and handles invalid username as a specific case

This commit is contained in:
Adam Brown 2022-03-11 12:52:09 +00:00
parent 373385b29f
commit 5aa35899bc
2 changed files with 26 additions and 18 deletions

View File

@ -58,6 +58,11 @@ fun Throwable.getRetryDelay(defaultValue: Long): Long {
?: defaultValue
}
fun Throwable.isInvalidUsername(): Boolean {
return this is Failure.ServerError &&
error.code == MatrixError.M_INVALID_USERNAME
}
fun Throwable.isInvalidPassword(): Boolean {
return this is Failure.ServerError &&
error.code == MatrixError.M_FORBIDDEN &&
@ -69,6 +74,16 @@ fun Throwable.isRegistrationDisabled(): Boolean {
httpCode == HttpsURLConnection.HTTP_FORBIDDEN
}
fun Throwable.isWeakPassword(): Boolean {
return this is Failure.ServerError && error.code == MatrixError.M_WEAK_PASSWORD
}
fun Throwable.isLoginEmailUnknown(): Boolean {
return this is Failure.ServerError &&
error.code == MatrixError.M_FORBIDDEN &&
error.message.isEmpty()
}
fun Throwable.isInvalidUIAAuth(): Boolean {
return this is Failure.ServerError &&
error.code == MatrixError.M_FORBIDDEN &&

View File

@ -44,10 +44,11 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.api.failure.isInvalidPassword
import org.matrix.android.sdk.api.failure.isInvalidUsername
import org.matrix.android.sdk.api.failure.isLoginEmailUnknown
import org.matrix.android.sdk.api.failure.isRegistrationDisabled
import org.matrix.android.sdk.api.failure.isWeakPassword
import reactivecircus.flowbinding.android.widget.textChanges
import javax.inject.Inject
@ -259,34 +260,26 @@ class FtueAuthLoginFragment @Inject constructor() : AbstractSSOFtueAuthFragment<
// Trick to display the error without text.
views.loginFieldTil.error = " "
when {
throwable is Failure.ServerError &&
throwable.error.code == MatrixError.M_FORBIDDEN &&
throwable.error.message.isEmpty() -> {
// Login with email, but email unknown
throwable.isInvalidUsername() -> {
views.loginFieldTil.error = errorFormatter.toHumanReadable(throwable)
}
throwable.isLoginEmailUnknown() -> {
views.loginFieldTil.error = getString(R.string.login_login_with_email_error)
}
throwable is Failure.ServerError && throwable.error.code == MatrixError.M_WEAK_PASSWORD -> {
views.passwordFieldTil.error = errorFormatter.toHumanReadable(throwable)
}
throwable.isInvalidPassword() && spaceInPassword() -> {
throwable.isInvalidPassword() && spaceInPassword() -> {
views.passwordFieldTil.error = getString(R.string.auth_invalid_login_param_space_in_password)
}
throwable.isInvalidPassword() -> {
throwable.isWeakPassword() || throwable.isInvalidPassword() -> {
views.passwordFieldTil.error = errorFormatter.toHumanReadable(throwable)
}
throwable.isRegistrationDisabled() -> {
throwable.isRegistrationDisabled() -> {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.dialog_title_error)
.setMessage(getString(R.string.login_registration_disabled))
.setPositiveButton(R.string.ok, null)
.show()
}
else -> {
else -> {
super.onError(throwable)
}
}