Fix: Indicate correct error message in case of loss of internet conne…#6736
Fix: Indicate correct error message in case of loss of internet conne…#673620020316 wants to merge 20 commits intocommons-app:mainfrom
Conversation
…ctivity on Android 14(commons-app#6311). Implemented appropriate error messages with the JavaRx library for network errors that occur during uploads, as previously the error messages were wrong. Try/catch did not work except for missing token exceptions.
…ctivity on Android 14(commons-app#6311). Removed gradle wrapper properties change and indentation changes from UploadClient.kt.
…ctivity on Android 14(commons-app#6311). Removed gradle wrapper properties change and indentation changes from UploadClient.kt.
Kota-Jagadeesh
left a comment
There was a problem hiding this comment.
@20020316 Could you fill in the remaining information in the PR description?
|
@Kota-Jagadeesh @RitikaPahwa4444 updated the PR. Could the pull request be approved now? |
|
I'm not sure if it's just me but GitHub isn't showing any further commits 😕 |
|
@RitikaPahwa4444 Now it should be visible. |
Date fixed.
| uploadResult.upload | ||
| }.onErrorResumeNext { e: Throwable -> | ||
| Timber.e(e, when (e) { | ||
| is InvalidLoginTokenException -> "The server could not retrieve the session token." |
There was a problem hiding this comment.
How about writing a helper function for this common code?
| else -> { | ||
| Timber.d("Upload stash failed %s", contribution.pageId) | ||
| Observable.just(StashUploadResult(StashUploadState.FAILED, null, null)) | ||
| Observable.just(StashUploadResult(StashUploadState.FAILED, null, "Network error")) |
There was a problem hiding this comment.
Are we sure it fails here due to a network error only?
There was a problem hiding this comment.
No, I do not know why it remained a network error. I had reversed it. I will commit again with null instead.
| countingRequestBody, | ||
| ).subscribe( | ||
| { uploadResult: UploadResult -> | ||
| { uploadResult: UploadResult? -> |
There was a problem hiding this comment.
uploadChunkToStash returns Observable<UploadResult>. It's not a nullable, is there anything that I'm missing out?
There was a problem hiding this comment.
So the uploadChunkToStash returned Observable<uploadResult> previously because the try catch block handled all exceptions as one, rather than dealing with them separately, so the compiler did not complain. The current code explicitly handles errors; it's better to handle the null exception separately from network errors. And to add, uploadChunksToStash maps its result to a variable of type UploadResult?, which is declared as such in UploadResponse.
There was a problem hiding this comment.
I noticed this PR adds onErrorResumeNext to the code, which makes it a nullable. Could you please elaborate in the PR description why this is necessary to fix the issue?
There was a problem hiding this comment.
I added the modified PR description per your request.
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists No newline at end of file | ||
| zipStorePath=wrapper/dists |
There was a problem hiding this comment.
Newline got removed/added, is it? GitHub is showing a diff here, so just confirming.
There was a problem hiding this comment.
I am unsure why the diff shows here, but I think it might be because I removed the initial line with the 2026 date, then added a new one at the top afterwards when reverting back to the 2023 date.
There was a problem hiding this comment.
Pull request overview
Improves upload failure reporting by distinguishing network/token/timeout errors during chunked stash uploads and final “upload from stash”, addressing misleading error messages when connectivity is lost (Issue #6311).
Changes:
- Refactors upload error handling to classify common network/token exceptions and surface more appropriate messages.
- Updates upload APIs/nullability around
UploadResultand addsonErrorResumeNextlogging/propagation. - Minor formatting normalization in Gradle wrapper properties.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
gradle/wrapper/gradle-wrapper.properties |
Minor properties formatting/line normalization. |
app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt |
Adds per-exception messaging and refactors Rx error handling for stash chunk upload + upload-from-stash. |
Comments suppressed due to low confidence (1)
app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt:286
uploadFileFromStashnow returnsObservable<UploadResult>but still force-unwrapsuniqueFileName!!/fileKey!!and callscsrfTokenClient.getTokenBlocking()eagerly. These can throw before anObservableis created, bypassing Rx error handling. Also, existing call sites handle errors by returningnullfromonErrorReturn, which will crash at runtime with RxJava (null emissions are not allowed) if this method’s contract is now non-null. Either (a) keep the return type nullable and handle null consistently, or (b) make inputs non-null + wrap creation inObservable.defer { ... }and update call sites to handle errors viaonErrorReturnItem/onErrorResumeNextwithout emitting null.
fun uploadFileFromStash(
contribution: Contribution?,
uniqueFileName: String?,
fileKey: String?,
): Observable<UploadResult> =
uploadInterface
.uploadFileFromStash(
csrfTokenClient.getTokenBlocking(),
pageContentsCreator.createFrom(contribution),
CommonsApplication.DEFAULT_EDIT_SUMMARY,
uniqueFileName!!,
fileKey!!,
).map { uploadResponse: JsonObject? ->
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| .map(UploadResponse::upload).onErrorResumeNext { e: Throwable -> | ||
| Timber.e(e, when (e) { | ||
| is InvalidLoginTokenException -> "The server could not retrieve the session token." | ||
| is UnknownHostException -> "Could not contact the remote host." | ||
| is SocketTimeoutException -> "The socket operation timed out while uploading." | ||
| is ConnectException -> "The connection was interrupted while uploading a chunk." | ||
| else -> "Unexpected error during chunk upload." | ||
| }) | ||
| Observable.error(e) | ||
| } |
|
@RitikaPahwa4444 Thank you for the GitHub Copilot review. I saw my mistakes and am working towards addressing them. |
…ctivity on Android 14(commons-app#6311). Added requested changes following review and testing(helper function, copilot review)
|
@RitikaPahwa4444 Pushed the changes requested right now, so both the changes indicated by the copilot review along with the helper function. |
Indentation changes reverted.
…ctivity on Android 14(commons-app#6311). Added requested changes following review and testing(helper function, copilot review)
# Conflicts: # app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt
Newline fixed.
|
@RitikaPahwa4444 It should be fine now, added strings to the xml file for localization(did not remove function since the class where it is used does not extend Context nor Activity). Indentation also fixed. |
| URLEncoder.encode(filename, "utf-8"), | ||
| countingRequestBody, | ||
| ) | ||
| fun uploadChunkToStash( |
There was a problem hiding this comment.
All the other functions seem to have additional space on the left, would you mind checking the indentation once?
There was a problem hiding this comment.
Fixed, uniform indentation in the file now.
There was a problem hiding this comment.
@20020316, instead of changing the indentation of the entire file, would it be possible to fix it just for the function you wished to change? The diff is really hard to go through.
There was a problem hiding this comment.
@RitikaPahwa4444 I reverted the indentation to the original version, that is, the unmodified version of the UploadClient.kt file, before the first commit. I hope it's fine now, as I assume you want to keep the indentation of the original, not my own, along with the added functionality for handling errors.
There was a problem hiding this comment.
I think it should be far easier to analyse the difference between the original and the new file now.
There was a problem hiding this comment.
@RitikaPahwa4444 The commit diff should now be clear. I fixed all indentation diff. Please check. If all is good, can the commits be approved for merge with the main branch and the issue closed?
…urce access for strings.(commons-app#6311)
Fixed an inadvertently changed string.
|
@RitikaPahwa4444 @Kota-Jagadeesh Could you please review the latest changes and, if all is ok, approve the merge and close the issue? |
Fixed newline in gradle.properties once more.
Final indentation fix for UploadClient.kt
Final indentation fix.
…ctivity on Android 14(#6311).
Description (required)
Fixes #6311
What changes did you make and why?
Implemented appropriate error messages with the onErrorResumeNext method from the JavaRx library. Previously, all errors were caught in a single try/catch block, regardless of type, with no distinction between network, token, or other unexpected errors. The onErrorResumeNext operator allows each error type to be handled separately with a specific message, sending them back to
Observable.error(e). This, in turn, also forces some other changes, namely setting explicitly nullable variables, e.gUploadResult->UploadResult?rather than leaving them to be non-nullable.Tests performed (required)
Tested {build variant, e.g. ProdDebug} on {name of device or emulator} with API level {API level}.
Tested on motorola edge 60, Android 16, API 36 and xiaomi redmi note 12 pro Android 14 API 34.
Screenshots (for UI changes only)


Need help? See https://support.google.com/android/answer/9075928
Note: Please ensure that you have read CONTRIBUTING.md if this is your first pull request.