From 7242cbda4010ad5eca54487691515d4ebc3012c5 Mon Sep 17 00:00:00 2001 From: onurays Date: Mon, 13 Apr 2020 17:48:19 +0300 Subject: [PATCH 1/2] Catch network errors during file downloading. Fixes #1229 --- CHANGES.md | 1 + .../matrix/android/internal/session/DefaultFileService.kt | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 16b39cc6b1..ef4f19fffe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -25,6 +25,7 @@ Bugfix 🐛: - Cross- Signing | After signin in new session, verification paper trail in DM is off (#1191) - Failed to encrypt message in room (message stays in red), [thanks to pwr22] (#925) - Cross-Signing | web <-> riotX After QR code scan, gossiping fails (#1210) + - Fix crash when trying to download file without internet connection (#1229) Translations 🗣: - diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt index 9eaea8cc34..6e6beecfb1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt @@ -19,6 +19,7 @@ package im.vector.matrix.android.internal.session import android.os.Environment import arrow.core.Try import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.extensions.tryThis import im.vector.matrix.android.api.session.content.ContentUrlResolver import im.vector.matrix.android.api.session.file.FileService import im.vector.matrix.android.api.util.Cancelable @@ -77,9 +78,10 @@ internal class DefaultFileService @Inject constructor( .url(resolvedUrl) .build() - val response = okHttpClient.newCall(request).execute() + val response = tryThis { okHttpClient.newCall(request).execute() } ?: return@flatMap Try.Failure(IOException()) var inputStream = response.body?.byteStream() Timber.v("Response size ${response.body?.contentLength()} - Stream available: ${inputStream?.available()}") + if (!response.isSuccessful || inputStream == null) { return@flatMap Try.Failure(IOException()) } From b4a3eb2cb33fb38388ba7e550260e336eea9b9bf Mon Sep 17 00:00:00 2001 From: onurays Date: Wed, 15 Apr 2020 16:39:41 +0300 Subject: [PATCH 2/2] Fix return the exception instead of empty one. --- .../matrix/android/internal/session/DefaultFileService.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt index 6e6beecfb1..66a1dcc9cc 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultFileService.kt @@ -19,7 +19,6 @@ package im.vector.matrix.android.internal.session import android.os.Environment import arrow.core.Try import im.vector.matrix.android.api.MatrixCallback -import im.vector.matrix.android.api.extensions.tryThis import im.vector.matrix.android.api.session.content.ContentUrlResolver import im.vector.matrix.android.api.session.file.FileService import im.vector.matrix.android.api.util.Cancelable @@ -78,7 +77,12 @@ internal class DefaultFileService @Inject constructor( .url(resolvedUrl) .build() - val response = tryThis { okHttpClient.newCall(request).execute() } ?: return@flatMap Try.Failure(IOException()) + val response = try { + okHttpClient.newCall(request).execute() + } catch (e: Throwable) { + return@flatMap Try.Failure(e) + } + var inputStream = response.body?.byteStream() Timber.v("Response size ${response.body?.contentLength()} - Stream available: ${inputStream?.available()}")