Skip to content

Commit 9238a20

Browse files
Fixes #4601 - 1. Handle possible exceptions in upload file from stash 2. Modify MWException, as error is nullable, update getTitle and getMessage to rever that (#4627)
1 parent 553a1d9 commit 9238a20

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

app/src/main/java/fr/free/nrw/commons/upload/UploadClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ public Observable<UploadResult> uploadFileFromStash(
218218
CommonsApplication.DEFAULT_EDIT_SUMMARY,
219219
uniqueFileName,
220220
fileKey).map(uploadResponse -> {
221-
UploadResponse uploadResult = gson
221+
final UploadResponse uploadResult = gson
222222
.fromJson(uploadResponse, UploadResponse.class);
223223
if (uploadResult.getUpload() == null) {
224224
final MwException exception = gson
225225
.fromJson(uploadResponse, MwException.class);
226226
Timber.e(exception, "Error in uploading file from stash");
227-
throw new RuntimeException(exception.getErrorCode());
227+
throw new Exception(exception.getErrorCode());
228228
}
229229
return uploadResult.getUpload();
230230
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package fr.free.nrw.commons.upload
22

3-
class UploadResponse(val upload: UploadResult)
3+
class UploadResponse(val upload: UploadResult?)

app/src/main/java/fr/free/nrw/commons/upload/worker/UploadWorker.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import fr.free.nrw.commons.contributions.ContributionDao
2121
import fr.free.nrw.commons.di.ApplicationlessInjection
2222
import fr.free.nrw.commons.location.LatLng
2323
import fr.free.nrw.commons.media.MediaClient
24+
import fr.free.nrw.commons.upload.StashUploadResult
2425
import fr.free.nrw.commons.upload.StashUploadState
2526
import fr.free.nrw.commons.upload.UploadClient
2627
import fr.free.nrw.commons.upload.UploadResult
@@ -265,7 +266,9 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
265266
//Upload the file to stash
266267
val stashUploadResult = uploadClient.uploadFileToStash(
267268
appContext, filename, contribution, notificationProgressUpdater
268-
).blockingSingle()
269+
).onErrorReturn{
270+
return@onErrorReturn StashUploadResult(StashUploadState.FAILED,fileKey = null)
271+
}.blockingSingle()
269272

270273
when (stashUploadResult.state) {
271274
StashUploadState.SUCCESS -> {
@@ -278,9 +281,11 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
278281
//Upload the file from stash
279282
val uploadResult = uploadClient.uploadFileFromStash(
280283
contribution, uniqueFileName, stashUploadResult.fileKey
281-
).blockingSingle()
284+
).onErrorReturn {
285+
return@onErrorReturn null
286+
}.blockingSingle()
282287

283-
if (uploadResult.isSuccessful()) {
288+
if (null != uploadResult && uploadResult.isSuccessful()) {
284289
Timber.d(
285290
"Stash Upload success..proceeding to make wikidata edit"
286291
)
@@ -313,6 +318,7 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
313318
Timber.e("Upload from stash failed for contribution : $filename")
314319
showFailedNotification(contribution)
315320
contribution.state=Contribution.STATE_FAILED
321+
contributionDao.saveSynchronous(contribution)
316322
if (STASH_ERROR_CODES.contains(exception.message)) {
317323
clearChunks(contribution)
318324
}

data-client/src/main/java/org/wikipedia/dataclient/mwapi/MwException.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ public String getErrorCode() {
3131
return error;
3232
}
3333

34-
@Nullable public String getTitle() {
35-
return error.getTitle();
34+
@Nullable
35+
public String getTitle() {
36+
if (error != null) {
37+
return error.getTitle();
38+
}
39+
return errors != null ? errors.get(0).getTitle() : null;
3640
}
3741

38-
@Override @Nullable public String getMessage() {
39-
return error.getDetails();
42+
@Override
43+
@Nullable
44+
public String getMessage() {
45+
if (error != null) {
46+
return error.getDetails();
47+
}
48+
return errors != null ? errors.get(0).getDetails() : null;
4049
}
4150
}

0 commit comments

Comments
 (0)