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.
This commit is contained in:
parent
7816529fd7
commit
efdc10e60a
144 changed files with 716 additions and 375 deletions
|
|
@ -21,6 +21,7 @@ import io.element.android.libraries.androidutils.media.runAndRelease
|
|||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.core.data.tryOrNull
|
||||
import io.element.android.libraries.core.extensions.mapFailure
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import io.element.android.libraries.core.mimetype.MimeTypes
|
||||
import io.element.android.libraries.core.mimetype.MimeTypes.isMimeTypeAudio
|
||||
import io.element.android.libraries.core.mimetype.MimeTypes.isMimeTypeImage
|
||||
|
|
@ -73,7 +74,7 @@ class AndroidMediaPreProcessor @Inject constructor(
|
|||
deleteOriginal: Boolean,
|
||||
compressIfPossible: Boolean,
|
||||
): Result<MediaUploadInfo> = withContext(coroutineDispatchers.computation) {
|
||||
runCatching {
|
||||
runCatchingExceptions {
|
||||
val result = when {
|
||||
// Special case for SVG, since Android can't read its metadata or create a thumbnail, it must be sent as a file
|
||||
mimeType == MimeTypes.Svg -> {
|
||||
|
|
@ -188,7 +189,7 @@ class AndroidMediaPreProcessor @Inject constructor(
|
|||
}
|
||||
|
||||
private suspend fun processVideo(uri: Uri, mimeType: String?, shouldBeCompressed: Boolean): MediaUploadInfo {
|
||||
val resultFile = runCatching {
|
||||
val resultFile = runCatchingExceptions {
|
||||
videoCompressor.compress(uri, shouldBeCompressed)
|
||||
.onEach {
|
||||
// TODO handle progress
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import io.element.android.libraries.androidutils.bitmap.resizeToMax
|
|||
import io.element.android.libraries.androidutils.bitmap.rotateToMetadataOrientation
|
||||
import io.element.android.libraries.androidutils.file.createTmpFile
|
||||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
|
|
@ -38,7 +39,7 @@ class ImageCompressor @Inject constructor(
|
|||
orientation: Int = ExifInterface.ORIENTATION_UNDEFINED,
|
||||
desiredQuality: Int = 78,
|
||||
): Result<ImageCompressionResult> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
runCatchingExceptions {
|
||||
val format = mimeTypeToCompressFormat(mimeType)
|
||||
val extension = mimeTypeToCompressFileExtension(mimeType)
|
||||
val compressedBitmap = compressToBitmap(inputStreamProvider, resizeMode, orientation).getOrThrow()
|
||||
|
|
@ -65,7 +66,7 @@ class ImageCompressor @Inject constructor(
|
|||
inputStreamProvider: () -> InputStream,
|
||||
resizeMode: ResizeMode,
|
||||
orientation: Int,
|
||||
): Result<Bitmap> = runCatching {
|
||||
): Result<Bitmap> = runCatchingExceptions {
|
||||
val options = BitmapFactory.Options()
|
||||
// Decode bounds
|
||||
inputStreamProvider().use { input ->
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import com.otaliastudios.transcoder.validator.WriteAlwaysValidator
|
|||
import io.element.android.libraries.androidutils.file.createTmpFile
|
||||
import io.element.android.libraries.androidutils.file.getMimeType
|
||||
import io.element.android.libraries.androidutils.file.safeDelete
|
||||
import io.element.android.libraries.core.extensions.runCatchingExceptions
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
|
|
@ -81,7 +82,7 @@ class VideoCompressor @Inject constructor(
|
|||
}
|
||||
|
||||
private fun getVideoMetadata(uri: Uri): VideoFileMetadata? {
|
||||
return runCatching {
|
||||
return runCatchingExceptions {
|
||||
MediaMetadataRetriever().use {
|
||||
it.setDataSource(context, uri)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue