diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/Contribution.kt b/app/src/main/java/fr/free/nrw/commons/contributions/Contribution.kt index be763fe8a6..64b405a4c5 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/Contribution.kt +++ b/app/src/main/java/fr/free/nrw/commons/contributions/Contribution.kt @@ -28,6 +28,9 @@ data class Contribution constructor( var dateCreatedSource: String? = null, var wikidataPlace: WikidataPlace? = null, var chunkInfo: ChunkInfo? = null, + // add error message to keep track of error message for failed upload + var errorMessage: String? = null, + var exceptionMessage: String? = null, /** * @return array list of entityids for the depictions */ diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java index 9b66556fc8..627b2ff8bd 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java @@ -43,6 +43,7 @@ import fr.free.nrw.commons.notification.NotificationController; import fr.free.nrw.commons.profile.ProfileActivity; import fr.free.nrw.commons.theme.BaseActivity; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -116,6 +117,19 @@ public class ContributionsFragment @Inject SessionManager sessionManager; + + private final List STASH_ERROR_CODES = Arrays.asList( + "file path is null", + "upload to stash failed" + ); + + private final List STASH_EXCEPTIONS = Arrays.asList( + "StashUploadException", + "IOException" + ); + + + private LatLng curLatLng; private boolean isFragmentAttachedBefore = false; @@ -670,12 +684,16 @@ public void retryUpload(Contribution contribution) { /* Limit the number of retries for a failed upload to handle cases like invalid filename as such uploads will never be successful */ - if(retries < MAX_RETRIES) { + if (contribution.getErrorMessage() != null && isGenuineError(contribution)) { + // we have a genuine error here therefore no retry + // TODO: notify user of error + + } else if(retries < MAX_RETRIES) { contribution.setRetries(retries + 1); Timber.d("Retried uploading %s %d times", contribution.getMedia().getFilename(), retries + 1); restartUpload(contribution); } else { - // TODO: Show the exact reason for failure + // TODO: notify user of error Toast.makeText(getContext(), R.string.retry_limit_reached, Toast.LENGTH_SHORT).show(); } @@ -686,6 +704,27 @@ public void retryUpload(Contribution contribution) { ViewUtil.showLongToast(getContext(), R.string.this_function_needs_network_connection); } + } + /** + * Check if the error that occurs is a genuine error + * @param contribution contribution to be retried + */ + public boolean isGenuineError(Contribution contribution) { + + //check against array for genuine errors + for (String error : STASH_ERROR_CODES) { + if (contribution.getErrorMessage().contains(error)) { + return true; + } + } + + for (String error : STASH_EXCEPTIONS) { + if (contribution.getExceptionMessage().contains(error)) { + return true; + } + } + return false; + } /** diff --git a/app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt b/app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt index 864e3149ad..f9c1e57b52 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt @@ -297,8 +297,10 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) : */ @SuppressLint("StringFormatInvalid") private suspend fun uploadContribution(contribution: Contribution) { + contribution.errorMessage = null if (contribution.localUri == null || contribution.localUri.path == null) { Timber.e("""upload: ${contribution.media.filename} failed, file path is null""") + contribution.errorMessage = "file path is null" } val media = contribution.media @@ -378,6 +380,9 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) : } else { Timber.e("Stash Upload failed") + if (uploadResult != null && contribution.errorMessage == null) { + contribution.errorMessage = uploadResult.result + } showFailedNotification(contribution) contribution.state = Contribution.STATE_FAILED contribution.chunkInfo = null @@ -385,6 +390,10 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) : } }catch (exception : Exception){ + if (contribution.errorMessage == null) { + contribution.errorMessage = "upload from stash failed with exception" + } + contribution.exceptionMessage = exception.toString() Timber.e(exception) Timber.e("Upload from stash failed for contribution : $filename") showFailedNotification(contribution) @@ -401,6 +410,9 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) : contributionDao.saveSynchronous(contribution) } else -> { + if (contribution.errorMessage == null) { + contribution.errorMessage = "upload to stash failed" + } Timber.e("""upload file to stash failed with status: ${stashUploadResult.state}""") showFailedNotification(contribution) contribution.state = Contribution.STATE_FAILED @@ -409,6 +421,10 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) : } } }catch (exception: Exception){ + if (contribution.errorMessage == null) { + contribution.errorMessage = "upload to stash failed with exception" + } + contribution.exceptionMessage = exception.toString() Timber.e(exception) Timber.e("Stash upload failed for contribution: $filename") showFailedNotification(contribution)