Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import java.net.UnknownHostException
import java.util.Base64
import javax.inject.Inject
import javax.inject.Singleton
import javax.net.ssl.SSLHandshakeException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.smartregister.fhircore.engine.configuration.app.ConfigService
Expand Down Expand Up @@ -163,12 +162,8 @@ constructor(
val oAuthResponse = oAuthService.fetchToken(body)
saveToken(username = username, password = password, oAuthResponse = oAuthResponse)
Result.success(oAuthResponse)
} catch (httpException: HttpException) {
Result.failure(httpException)
} catch (unknownHostException: UnknownHostException) {
Result.failure(unknownHostException)
} catch (sslHandShakeException: SSLHandshakeException) {
Result.failure(sslHandShakeException)
} catch (e: Exception) {
Result.failure(e)
Comment on lines 166 to +173
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.

fetchAccessToken now catches Exception, which will also catch kotlinx.coroutines.CancellationException and prevent cooperative cancellation (and can cause structured-concurrency bugs). Consider adding a catch (e: CancellationException) { throw e } before this, and narrowing the generic catch to IOException (plus HttpException) to avoid masking programmer errors while still covering ConnectException, SocketTimeoutException, UnknownHostException, SSLHandshakeException, etc.

Copilot uses AI. Check for mistakes.
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.

@allan-on Can you apply this change it is okay.

}
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,26 @@ 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")
)

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
2 changes: 1 addition & 1 deletion android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ espresso-core = "3.6.1"
fhir-sdk-common = "0.1.0-alpha05-preview3-SNAPSHOT"
fhir-sdk-contrib-barcode = "0.1.0-beta3-preview7-SNAPSHOT"
fhir-sdk-contrib-locationwidget = "0.1.0-alpha01-preview2-SNAPSHOT"
fhir-sdk-data-capture = "1.3.0-preview-SNAPSHOT"
fhir-sdk-data-capture = "1.3.0-preview1-SNAPSHOT"
fhir-sdk-engine = "1.2.0-preview-SNAPSHOT"
fhir-sdk-knowledge = "0.1.0-beta01-preview1-SNAPSHOT"
fhir-sdk-workflow = "0.1.0-beta01-preview2-SNAPSHOT"
Expand Down
Loading