Skip to content

Fix: Indicate correct error message in case of loss of internet conne…#6736

Open
20020316 wants to merge 20 commits intocommons-app:mainfrom
20020316:main
Open

Fix: Indicate correct error message in case of loss of internet conne…#6736
20020316 wants to merge 20 commits intocommons-app:mainfrom
20020316:main

Conversation

@20020316
Copy link

@20020316 20020316 commented Mar 14, 2026

…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.g UploadResult -> 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)
screenshot_1 1
Screenshot_2

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.

…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.
RaresNicoMoldo and others added 4 commits March 14, 2026 17:02
…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.
Copy link
Collaborator

@Kota-Jagadeesh Kota-Jagadeesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@20020316 Could you fill in the remaining information in the PR description?

@20020316
Copy link
Author

@Kota-Jagadeesh @RitikaPahwa4444 updated the PR. Could the pull request be approved now?

@RitikaPahwa4444
Copy link
Collaborator

I'm not sure if it's just me but GitHub isn't showing any further commits 😕

@20020316
Copy link
Author

20020316 commented Mar 14, 2026

@RitikaPahwa4444 Now it should be visible.

uploadResult.upload
}.onErrorResumeNext { e: Throwable ->
Timber.e(e, when (e) {
is InvalidLoginTokenException -> "The server could not retrieve the session token."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about writing a helper function for this common code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed.

else -> {
Timber.d("Upload stash failed %s", contribution.pageId)
Observable.just(StashUploadResult(StashUploadState.FAILED, null, null))
Observable.just(StashUploadResult(StashUploadState.FAILED, null, "Network error"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure it fails here due to a network error only?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uploadChunkToStash returns Observable<UploadResult>. It's not a nullable, is there anything that I'm missing out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

@RitikaPahwa4444 RitikaPahwa4444 Mar 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline got removed/added, is it? GitHub is showing a diff here, so just confirming.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UploadResult and adds onErrorResumeNext logging/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

  • uploadFileFromStash now returns Observable<UploadResult> but still force-unwraps uniqueFileName!!/fileKey!! and calls csrfTokenClient.getTokenBlocking() eagerly. These can throw before an Observable is created, bypassing Rx error handling. Also, existing call sites handle errors by returning null from onErrorReturn, 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 in Observable.defer { ... } and update call sites to handle errors via onErrorReturnItem/onErrorResumeNext without 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.

Comment on lines +257 to +266
.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)
}
@20020316
Copy link
Author

@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)
@20020316
Copy link
Author

@RitikaPahwa4444 Pushed the changes requested right now, so both the changes indicated by the copilot review along with the helper function.

20020316 and others added 4 commits March 15, 2026 15:57
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
@20020316
Copy link
Author

@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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other functions seem to have additional space on the left, would you mind checking the indentation once?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, uniform indentation in the file now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, yes.

Copy link
Author

@20020316 20020316 Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be far easier to analyse the difference between the original and the new file now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

RaresNicoMoldo and others added 2 commits March 15, 2026 19:08
@20020316
Copy link
Author

20020316 commented Mar 16, 2026

@RitikaPahwa4444 @Kota-Jagadeesh Could you please review the latest changes and, if all is ok, approve the merge and close the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: incorrect error when Internet is not working

5 participants