From d49651fdcc3acffab8fbe8c42f177e54c56d01a9 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 16 Sep 2019 20:01:24 +0300 Subject: [PATCH] Improved uploads error handling --- .../mthmmy/activities/upload/UploadActivity.java | 2 +- .../multipart/MultipartUploadException.java | 8 ++++++++ .../thmmy/mthmmy/services/UploadsReceiver.java | 16 +++++++++++++--- .../mthmmy/utils/parsing/ParseException.java | 5 ++--- 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/activities/upload/multipart/MultipartUploadException.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java index 98c23db9..347acdc7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java @@ -58,6 +58,7 @@ import java.util.Locale; import java.util.UUID; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.activities.upload.multipart.MultipartUploadRequest; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.model.UploadCategory; @@ -66,7 +67,6 @@ import gr.thmmy.mthmmy.services.UploadsReceiver; import gr.thmmy.mthmmy.utils.AppCompatSpinnerWithoutDefault; import gr.thmmy.mthmmy.utils.FileUtils; import gr.thmmy.mthmmy.utils.TakePhoto; -import gr.thmmy.mthmmy.activities.upload.multipart.MultipartUploadRequest; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/multipart/MultipartUploadException.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/multipart/MultipartUploadException.java new file mode 100644 index 00000000..0b8cdc29 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/multipart/MultipartUploadException.java @@ -0,0 +1,8 @@ +package gr.thmmy.mthmmy.activities.upload.multipart; + +public class MultipartUploadException extends RuntimeException { + public MultipartUploadException(String message) { + super(message); + } + +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java b/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java index f8b06e28..015dc91d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/UploadsReceiver.java @@ -22,8 +22,10 @@ import net.gotev.uploadservice.UploadServiceBroadcastReceiver; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.upload.UploadsHelper; +import gr.thmmy.mthmmy.activities.upload.multipart.MultipartUploadException; import gr.thmmy.mthmmy.base.BaseApplication; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import timber.log.Timber; public class UploadsReceiver extends UploadServiceBroadcastReceiver { public static final String UPLOAD_ID_KEY = "UPLOAD_ID_KEY"; @@ -153,7 +155,7 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver { TextView dialogProgressText = progressWindow.findViewById(R.id.dialog_upload_progress_text); dialogProgressBar.setVisibility(View.GONE); - dialogProgressText.setText("Upload failed."); + dialogProgressText.setText("Upload failed"); } if (uploadInfo.getUploadedBytes() == uploadInfo.getTotalBytes()) { @@ -185,12 +187,20 @@ public class UploadsReceiver extends UploadServiceBroadcastReceiver { dialogUploadID = null; } - Toast.makeText(context.getApplicationContext(), "Upload completed successfully", Toast.LENGTH_SHORT).show(); + String response = serverResponse.getBodyAsString(); + if(response.contains("Η προσθήκη του αρχείου ήταν επιτυχημένη.")||response.contains("The upload was successful.")){ + Toast.makeText(context.getApplicationContext(), "Upload completed successfully", Toast.LENGTH_SHORT).show(); + BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null); + } + else { + Timber.e(new MultipartUploadException(response)); + onError(context,uploadInfo,serverResponse,new MultipartUploadException(response)); + } + if (storage == null) { storage = new Storage(context.getApplicationContext()); } - BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null); UploadsHelper.deleteTempFiles(storage); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java index 942e789b..5fc06664 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java @@ -1,13 +1,12 @@ package gr.thmmy.mthmmy.utils.parsing; /** - * ParseException is to be used for errors while parsing. + * Use ParseException for errors while parsing. */ public class ParseException extends RuntimeException { public ParseException() {} - public ParseException(String message) - { + public ParseException(String message) { super(message); }