Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ constructor(
Result.failure(unknownHostException)
} catch (sslHandShakeException: SSLHandshakeException) {
Result.failure(sslHandShakeException)
} catch (e: Exception) {
Result.failure(e)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of omitting all these, you can retain them and add.

catch (e: Exception) {
  Result.failure(e)
}

At the end, to catch any missed exceptions.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,35 +268,27 @@ class TokenAuthenticatorTest : RobolectricTest() {

@Test
@ExperimentalCoroutinesApi
fun testFetchTokenShouldShouldCatchHttpAndUnknownHostAndSSLHandshakeExceptions() {
fun testFetchAccessTokenShouldCatchAllNetworkExceptions() {
val username = sampleUsername
val password = charArrayOf('P', '4', '5', '5', 'W', '4', '0')

val httpException = HttpException(Response.success(null))

coEvery { oAuthService.fetchToken(any()) }.throws(httpException)

runTest {
var result = tokenAuthenticator.fetchAccessToken(username, password)
Assert.assertEquals(Result.failure<HttpException>(httpException), result)
}

val unknownHostException = UnknownHostException()

coEvery { oAuthService.fetchToken(any()) }.throws(unknownHostException)

runTest {
var result = tokenAuthenticator.fetchAccessToken(username, password)
Assert.assertEquals(Result.failure<UnknownHostException>(unknownHostException), result)
}

val sslHandshakeException = SSLHandshakeException("reason")
// Test with various exception types to verify generic exception handling
val exceptions =
listOf(
HttpException(Response.error<OAuthResponse>(401, mockk(relaxed = true))),
UnknownHostException(),
SSLHandshakeException("SSL error"),
IOException("IO error"),
)
Comment on lines +275 to +282
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test aims to cover “all network exceptions”, but it doesn’t include the ConnectException that triggered the reported crash (issue #3821). Add ConnectException (and ideally SocketTimeoutException) to this list so the regression is directly covered.

Copilot uses AI. Check for mistakes.

coEvery { oAuthService.fetchToken(any()) }.throws(sslHandshakeException)
exceptions.forEach { exception ->
coEvery { oAuthService.fetchToken(any()) }.throws(exception)

runTest {
var result = tokenAuthenticator.fetchAccessToken(username, password)
Assert.assertEquals(Result.failure<SSLHandshakeException>(sslHandshakeException), result)
runTest {
val result = tokenAuthenticator.fetchAccessToken(username, password)
Assert.assertTrue(result.isFailure)
Assert.assertEquals(exception, result.exceptionOrNull())
}
}
}

Expand Down
Loading