Skip to content

Commit

Permalink
AND-19988 Add Firebase Crashlytics logs for login connectivity issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mega-ht committed Nov 21, 2024
1 parent fbc8226 commit f10a001
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
Expand All @@ -29,6 +30,7 @@ import mega.privacy.android.data.mapper.login.FetchNodesUpdateMapper
import mega.privacy.android.data.mapper.login.TemporaryWaitingErrorMapper
import mega.privacy.android.domain.entity.login.FetchNodesUpdate
import mega.privacy.android.domain.entity.login.LoginStatus
import mega.privacy.android.domain.entity.login.TemporaryWaitingError
import mega.privacy.android.domain.exception.ChatLoggingOutException
import mega.privacy.android.domain.exception.ChatNotInitializedErrorStatus
import mega.privacy.android.domain.exception.ChatNotInitializedUnknownStatus
Expand All @@ -43,6 +45,7 @@ import mega.privacy.android.domain.exception.LoginWrongMultiFactorAuth
import mega.privacy.android.domain.exception.login.FetchNodesBlockedAccount
import mega.privacy.android.domain.exception.login.FetchNodesErrorAccess
import mega.privacy.android.domain.exception.login.FetchNodesUnknownStatus
import mega.privacy.android.domain.monitoring.CrashReporter
import mega.privacy.android.domain.qualifier.ApplicationScope
import mega.privacy.android.domain.qualifier.IoDispatcher
import mega.privacy.android.domain.repository.security.LoginRepository
Expand Down Expand Up @@ -74,6 +77,7 @@ internal class DefaultLoginRepository @Inject constructor(
@ApplicationScope private val applicationScope: CoroutineScope,
private val setLogoutFlagWrapper: SetLogoutFlagWrapper,
private val credentialsPreferencesGateway: Lazy<CredentialsPreferencesGateway>,
private val crashReporter: CrashReporter
) : LoginRepository {

override suspend fun initMegaChat(session: String) =
Expand Down Expand Up @@ -234,6 +238,9 @@ internal class DefaultLoginRepository @Inject constructor(
Timber.w("Waiting, retry reason for ${request.requestString}: $it")
temporaryWaitingErrorMapper(it)
}
if (this.isActive && temporaryError == TemporaryWaitingError.ConnectivityIssues) {
crashReporter.report(Throwable("Connection issue occurred during login. Error code: ${error.errorCode}, waiting reason: MegaApiJava.RETRY_CONNECTIVITY"))
}
trySend(LoginStatus.LoginWaiting(temporaryError))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import mega.privacy.android.domain.exception.LoginWrongMultiFactorAuth
import mega.privacy.android.domain.exception.login.FetchNodesBlockedAccount
import mega.privacy.android.domain.exception.login.FetchNodesErrorAccess
import mega.privacy.android.domain.exception.login.FetchNodesUnknownStatus
import mega.privacy.android.domain.monitoring.CrashReporter
import nz.mega.sdk.MegaApiJava
import nz.mega.sdk.MegaChatApi
import nz.mega.sdk.MegaError
Expand Down Expand Up @@ -69,6 +70,7 @@ class DefaultLoginRepositoryTest {
private val temporaryWaitingErrorMapper = mock<TemporaryWaitingErrorMapper>()
private val setLogoutFlagWrapper = mock<SetLogoutFlagWrapper>()
private val credentialsPreferencesGateway = mock<CredentialsPreferencesGateway>()
private val crashReporter = mock<CrashReporter>()
private val testScope = CoroutineScope(UnconfinedTestDispatcher())

private val email = "test@email.com"
Expand All @@ -89,7 +91,8 @@ class DefaultLoginRepositoryTest {
temporaryWaitingErrorMapper = temporaryWaitingErrorMapper,
applicationScope = testScope,
setLogoutFlagWrapper = setLogoutFlagWrapper,
credentialsPreferencesGateway = { credentialsPreferencesGateway }
credentialsPreferencesGateway = { credentialsPreferencesGateway },
crashReporter = crashReporter
)
}

Expand Down Expand Up @@ -532,6 +535,32 @@ class DefaultLoginRepositoryTest {
}
}


@Test
fun `test that log is sent to Crashlytics when login fails with connectiviing issue`() =
runTest {
val listenerCaptor = argumentCaptor<OptionalMegaRequestListenerInterface>()
val request = mock<MegaRequest>()
val error = mock<MegaError> { on { errorCode }.thenReturn(MegaError.API_EAGAIN) }

whenever(megaApiGateway.getWaitingReason()).thenReturn(1)
whenever(temporaryWaitingErrorMapper(1)).thenReturn(
TemporaryWaitingError.ConnectivityIssues
)

underTest.fastLoginFlow(session).test {
verify(megaApiGateway).fastLogin(any(), listenerCaptor.capture())
listenerCaptor.firstValue.onRequestTemporaryError(mock(), request, error)
val result = awaitItem()
assertThat(result).isInstanceOf(LoginStatus.LoginWaiting::class.java)
assertThat((result as LoginStatus.LoginWaiting).error).isEqualTo(
TemporaryWaitingError.ConnectivityIssues
)
verify(crashReporter).report(any())
cancelAndIgnoreRemainingEvents()
}
}

@Test
fun `test that fast login flow returns LoginLoggedOutFromOtherLocation if the request fails with API_ESID`() =
runTest {
Expand Down

0 comments on commit f10a001

Please sign in to comment.