From 9538804ba09edf45de57bacc12f9e6dc01c56e6c Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 29 Dec 2018 16:10:21 +0000 Subject: [PATCH] Improved error handling --- .../eu/faircode/email/FragmentAccount.java | 40 +++++++++++-------- .../eu/faircode/email/FragmentIdentity.java | 13 +++--- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index 9d257a3bf5..abe43fd924 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -447,13 +447,13 @@ public class FragmentAccount extends FragmentEx { int auth_type = args.getInt("auth_type"); if (TextUtils.isEmpty(host)) - throw new Throwable(context.getString(R.string.title_no_host)); + throw new IllegalArgumentException(context.getString(R.string.title_no_host)); if (TextUtils.isEmpty(port)) port = (starttls ? "143" : "993"); if (TextUtils.isEmpty(user)) - throw new Throwable(context.getString(R.string.title_no_user)); + throw new IllegalArgumentException(context.getString(R.string.title_no_user)); if (TextUtils.isEmpty(password) && !insecure) - throw new Throwable(context.getString(R.string.title_no_password)); + throw new IllegalArgumentException(context.getString(R.string.title_no_password)); CheckResult result = new CheckResult(); result.folders = new ArrayList<>(); @@ -550,11 +550,14 @@ public class FragmentAccount extends FragmentEx { grpFolders.setVisibility(View.GONE); btnSave.setVisibility(View.GONE); - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(Helper.formatThrowable(ex)) - .setPositiveButton(android.R.string.cancel, null) - .create() - .show(); + if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) + .setMessage(Helper.formatThrowable(ex)) + .setPositiveButton(android.R.string.cancel, null) + .create() + .show(); } }.load(FragmentAccount.this, args); } @@ -645,17 +648,17 @@ public class FragmentAccount extends FragmentEx { EntityFolder junk = (EntityFolder) args.getSerializable("junk"); if (TextUtils.isEmpty(host)) - throw new Throwable(context.getString(R.string.title_no_host)); + throw new IllegalArgumentException(context.getString(R.string.title_no_host)); if (TextUtils.isEmpty(port)) port = (starttls ? "143" : "993"); if (TextUtils.isEmpty(user)) - throw new Throwable(context.getString(R.string.title_no_user)); + throw new IllegalArgumentException(context.getString(R.string.title_no_user)); if (synchronize && TextUtils.isEmpty(password) && !insecure) - throw new Throwable(context.getString(R.string.title_no_password)); + throw new IllegalArgumentException(context.getString(R.string.title_no_password)); if (TextUtils.isEmpty(interval)) interval = "19"; if (synchronize && drafts == null) - throw new Throwable(context.getString(R.string.title_no_drafts)); + throw new IllegalArgumentException(context.getString(R.string.title_no_drafts)); if (Color.TRANSPARENT == color) color = null; @@ -837,11 +840,14 @@ public class FragmentAccount extends FragmentEx { btnSave.setEnabled(true); pbSave.setVisibility(View.GONE); - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(Helper.formatThrowable(ex)) - .setPositiveButton(android.R.string.cancel, null) - .create() - .show(); + if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) + .setMessage(Helper.formatThrowable(ex)) + .setPositiveButton(android.R.string.cancel, null) + .create() + .show(); } }.load(FragmentAccount.this, args); } diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index 61ff8e5801..1a45319852 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -600,11 +600,14 @@ public class FragmentIdentity extends FragmentEx { btnSave.setEnabled(true); pbSave.setVisibility(View.GONE); - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(Helper.formatThrowable(ex)) - .setPositiveButton(android.R.string.cancel, null) - .create() - .show(); + if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) + .setMessage(Helper.formatThrowable(ex)) + .setPositiveButton(android.R.string.cancel, null) + .create() + .show(); } }.load(FragmentIdentity.this, args); }