Skip to content

Commit efdc10e

Browse files
authored
Add catchingExceptions method to replace runCatching (#4797)
- Add `runCatchingExceptions` and `mapCatchingExceptions` to replace `runCatching` and `mapCatching`. - Make `tryOrNull { ... }` catch only exceptions too. - Apply the changes to the whole project. - Add new Rust fakes for tests to handle the code that's now unblocked - previously it just threw an `UnsatisfiedLinkError` which we ignored. - Add a new `detekt-rules` project with a `RunCatchingRule` to prevent `runCatching` and `mapCatching` usages.
1 parent 7816529 commit efdc10e

File tree

144 files changed

+716
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+716
-375
lines changed

appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInPresenter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import im.vector.app.features.analytics.plan.CryptoSessionStateChange
2121
import im.vector.app.features.analytics.plan.UserProperties
2222
import io.element.android.libraries.architecture.AsyncData
2323
import io.element.android.libraries.architecture.Presenter
24+
import io.element.android.libraries.core.extensions.runCatchingExceptions
2425
import io.element.android.libraries.core.log.logger.LoggerTag
2526
import io.element.android.libraries.core.meta.BuildMeta
2627
import io.element.android.libraries.matrix.api.MatrixClient
@@ -125,7 +126,7 @@ class LoggedInPresenter @Inject constructor(
125126
}
126127

127128
// Force the user to log out if they were using the proxy sliding sync as it's no longer supported by the SDK
128-
private suspend fun MatrixClient.needsForcedNativeSlidingSyncMigration(): Result<Boolean> = runCatching {
129+
private suspend fun MatrixClient.needsForcedNativeSlidingSyncMigration(): Result<Boolean> = runCatchingExceptions {
129130
val currentSlidingSyncVersion = currentSlidingSyncVersion().getOrThrow()
130131
currentSlidingSyncVersion == SlidingSyncVersion.Proxy
131132
}

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ allprojects {
5050
}
5151
dependencies {
5252
detektPlugins("io.nlopez.compose.rules:detekt:0.4.22")
53+
detektPlugins(project(":tests:detekt-rules"))
5354
}
5455

5556
tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {

features/cachecleaner/impl/src/main/kotlin/io/element/android/features/cachecleaner/impl/DefaultCacheCleaner.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package io.element.android.features.cachecleaner.impl
1010
import com.squareup.anvil.annotations.ContributesBinding
1111
import io.element.android.features.cachecleaner.api.CacheCleaner
1212
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
13+
import io.element.android.libraries.core.extensions.runCatchingExceptions
1314
import io.element.android.libraries.di.AppScope
1415
import io.element.android.libraries.di.CacheDirectory
1516
import kotlinx.coroutines.CoroutineScope
@@ -33,7 +34,7 @@ class DefaultCacheCleaner @Inject constructor(
3334

3435
override fun clearCache() {
3536
scope.launch(dispatchers.io) {
36-
runCatching {
37+
runCatchingExceptions {
3738
SUBDIRS_TO_CLEANUP.forEach {
3839
File(cacheDir.path, it).apply {
3940
if (exists()) {

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/services/CallForegroundService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.core.content.ContextCompat
2424
import androidx.core.graphics.drawable.IconCompat
2525
import io.element.android.features.call.impl.R
2626
import io.element.android.features.call.impl.ui.ElementCallActivity
27+
import io.element.android.libraries.core.extensions.runCatchingExceptions
2728
import io.element.android.libraries.designsystem.utils.CommonDrawables
2829
import io.element.android.libraries.push.api.notifications.ForegroundServiceType
2930
import io.element.android.libraries.push.api.notifications.NotificationIdProvider
@@ -78,7 +79,7 @@ class CallForegroundService : Service() {
7879
} else {
7980
0
8081
}
81-
runCatching {
82+
runCatchingExceptions {
8283
ServiceCompat.startForeground(this, notificationId, notification, serviceType)
8384
}.onFailure {
8485
Timber.e(it, "Failed to start ongoing call foreground service")

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/ActiveCallManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.element.android.features.call.api.CallType
2121
import io.element.android.features.call.api.CurrentCall
2222
import io.element.android.features.call.impl.notifications.CallNotificationData
2323
import io.element.android.features.call.impl.notifications.RingingCallNotificationCreator
24+
import io.element.android.libraries.core.extensions.runCatchingExceptions
2425
import io.element.android.libraries.di.AppScope
2526
import io.element.android.libraries.di.ApplicationContext
2627
import io.element.android.libraries.di.SingleIn
@@ -218,7 +219,7 @@ class DefaultActiveCallManager @Inject constructor(
218219
timestamp = notificationData.timestamp,
219220
textContent = notificationData.textContent,
220221
) ?: return
221-
runCatching {
222+
runCatchingExceptions {
222223
notificationManagerCompat.notify(
223224
NotificationIdProvider.getForegroundServiceNotificationId(ForegroundServiceType.INCOMING_CALL),
224225
notification,

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/DefaultCallWidgetProvider.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package io.element.android.features.call.impl.utils
99

1010
import com.squareup.anvil.annotations.ContributesBinding
11+
import io.element.android.libraries.core.extensions.runCatchingExceptions
1112
import io.element.android.libraries.di.AppScope
1213
import io.element.android.libraries.matrix.api.MatrixClientProvider
1314
import io.element.android.libraries.matrix.api.core.RoomId
@@ -33,7 +34,7 @@ class DefaultCallWidgetProvider @Inject constructor(
3334
clientId: String,
3435
languageTag: String?,
3536
theme: String?,
36-
): Result<CallWidgetProvider.GetWidgetResult> = runCatching {
37+
): Result<CallWidgetProvider.GetWidgetResult> = runCatchingExceptions {
3738
val matrixClient = matrixClientsProvider.getOrRestore(sessionId).getOrThrow()
3839
val room = activeRoomsHolder.getActiveRoomMatching(sessionId, roomId)
3940
?: matrixClient.getJoinedRoom(roomId)

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/WidgetMessageSerializer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
package io.element.android.features.call.impl.utils
99

1010
import io.element.android.features.call.impl.data.WidgetMessage
11+
import io.element.android.libraries.core.extensions.runCatchingExceptions
1112
import kotlinx.serialization.json.Json
1213

1314
object WidgetMessageSerializer {
1415
private val coder = Json { ignoreUnknownKeys = true }
1516

1617
fun deserialize(message: String): Result<WidgetMessage> {
17-
return runCatching { coder.decodeFromString(WidgetMessage.serializer(), message) }
18+
return runCatchingExceptions { coder.decodeFromString(WidgetMessage.serializer(), message) }
1819
}
1920

2021
fun serialize(message: WidgetMessage): String {

features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootStateProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ open class CreateRoomRootStateProvider : PreviewParameterProvider<CreateRoomRoot
3535
}
3636
),
3737
aCreateRoomRootState(
38-
startDmAction = AsyncAction.Failure(Throwable("error")),
38+
startDmAction = AsyncAction.Failure(RuntimeException("error")),
3939
userListState = aMatrixUser().let {
4040
aUserListState().copy(
4141
searchQuery = it.userId.value,

features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/DefaultStartDMActionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import io.element.android.features.createroom.api.ConfirmingStartDmWithMatrixUse
1414
import io.element.android.libraries.architecture.AsyncAction
1515
import io.element.android.libraries.matrix.api.MatrixClient
1616
import io.element.android.libraries.matrix.api.core.RoomId
17+
import io.element.android.libraries.matrix.test.AN_EXCEPTION
1718
import io.element.android.libraries.matrix.test.A_ROOM_ID
18-
import io.element.android.libraries.matrix.test.A_THROWABLE
1919
import io.element.android.libraries.matrix.test.FakeMatrixClient
2020
import io.element.android.libraries.matrix.ui.components.aMatrixUser
2121
import io.element.android.services.analytics.api.AnalyticsService
@@ -70,13 +70,13 @@ class DefaultStartDMActionTest {
7070
fun `when dm creation fails, assert state is updated with given error`() = runTest {
7171
val matrixClient = FakeMatrixClient().apply {
7272
givenFindDmResult(null)
73-
givenCreateDmResult(Result.failure(A_THROWABLE))
73+
givenCreateDmResult(Result.failure(AN_EXCEPTION))
7474
}
7575
val analyticsService = FakeAnalyticsService()
7676
val action = createStartDMAction(matrixClient, analyticsService)
7777
val state = mutableStateOf<AsyncAction<RoomId>>(AsyncAction.Uninitialized)
7878
action.execute(aMatrixUser(), true, state)
79-
assertThat(state.value).isEqualTo(AsyncAction.Failure(A_THROWABLE))
79+
assertThat(state.value).isEqualTo(AsyncAction.Failure(AN_EXCEPTION))
8080
assertThat(analyticsService.capturedEvents).isEmpty()
8181
}
8282

features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/configureroom/ConfigureBaseRoomPresenterTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import io.element.android.libraries.matrix.api.core.RoomId
2222
import io.element.android.libraries.matrix.api.room.alias.ResolvedRoomAlias
2323
import io.element.android.libraries.matrix.api.room.alias.RoomAliasHelper
2424
import io.element.android.libraries.matrix.test.AN_AVATAR_URL
25+
import io.element.android.libraries.matrix.test.AN_EXCEPTION
2526
import io.element.android.libraries.matrix.test.A_MESSAGE
2627
import io.element.android.libraries.matrix.test.A_ROOM_ID
2728
import io.element.android.libraries.matrix.test.A_ROOM_NAME
28-
import io.element.android.libraries.matrix.test.A_THROWABLE
2929
import io.element.android.libraries.matrix.test.FakeMatrixClient
3030
import io.element.android.libraries.matrix.test.room.alias.FakeRoomAliasHelper
3131
import io.element.android.libraries.matrix.ui.components.aMatrixUser
@@ -274,7 +274,7 @@ class ConfigureBaseRoomPresenterTest {
274274
createRoomDataStore.setAvatarUri(Uri.parse(AN_URI_FROM_GALLERY))
275275
skipItems(1)
276276
mediaPreProcessor.givenResult(Result.success(MediaUploadInfo.Image(mockk(), mockk(), mockk())))
277-
matrixClient.givenUploadMediaResult(Result.failure(A_THROWABLE))
277+
matrixClient.givenUploadMediaResult(Result.failure(AN_EXCEPTION))
278278

279279
initialState.eventSink(ConfigureRoomEvents.CreateRoom)
280280
assertThat(awaitItem().createRoomAction).isInstanceOf(AsyncAction.Loading::class.java)
@@ -298,7 +298,7 @@ class ConfigureBaseRoomPresenterTest {
298298
)
299299
presenter.test {
300300
val initialState = initialState()
301-
val createRoomResult = Result.failure<RoomId>(A_THROWABLE)
301+
val createRoomResult = Result.failure<RoomId>(AN_EXCEPTION)
302302

303303
fakeMatrixClient.givenCreateRoomResult(createRoomResult)
304304

0 commit comments

Comments
 (0)