From 286a6a9720eb581887ce69b2ea1be066a88ecabd Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 26 Jun 2019 16:51:29 +0200 Subject: [PATCH] Conditionally sanitize exceptions --- .../eu/faircode/email/ActivityBilling.java | 2 +- .../java/eu/faircode/email/ActivityView.java | 2 +- app/src/main/java/eu/faircode/email/Core.java | 8 ++--- .../eu/faircode/email/FragmentAccount.java | 2 +- .../eu/faircode/email/FragmentIdentity.java | 2 +- .../eu/faircode/email/FragmentMessages.java | 2 +- .../eu/faircode/email/FragmentQuickSetup.java | 2 +- .../main/java/eu/faircode/email/Helper.java | 36 +++++++++++-------- .../java/eu/faircode/email/MessageHelper.java | 18 +++++----- .../java/eu/faircode/email/ServiceSend.java | 2 +- .../eu/faircode/email/ServiceSynchronize.java | 14 ++++---- 11 files changed, 48 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityBilling.java b/app/src/main/java/eu/faircode/email/ActivityBilling.java index b9147672ba..c4260558df 100644 --- a/app/src/main/java/eu/faircode/email/ActivityBilling.java +++ b/app/src/main/java/eu/faircode/email/ActivityBilling.java @@ -332,7 +332,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL } } catch (Throwable ex) { Log.e(ex); - Toast.makeText(this, Helper.formatThrowable(ex), Toast.LENGTH_LONG).show(); + Toast.makeText(this, Helper.formatThrowable(ex, false), Toast.LENGTH_LONG).show(); } editor.apply(); diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index aaab86dd03..f0fa05a515 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -633,7 +633,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB @Override protected void onException(Bundle args, Throwable ex) { - Toast.makeText(ActivityView.this, Helper.formatThrowable(ex), Toast.LENGTH_LONG).show(); + Toast.makeText(ActivityView.this, Helper.formatThrowable(ex, false), Toast.LENGTH_LONG).show(); } }.execute(this, new Bundle(), "crash:log"); } diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 786dd75e23..5180b7aba1 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -246,7 +246,7 @@ class Core { db.operation().deleteOperation(op.id); } catch (Throwable ex) { Log.e(folder.name, ex); - EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex)); + EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex, false)); db.operation().setOperationError(op.id, Helper.formatThrowable(ex)); if (message != null && !(ex instanceof IllegalArgumentException)) @@ -1075,7 +1075,7 @@ class Core { Log.w(folder.name, ex); } catch (Throwable ex) { Log.e(folder.name, ex); - EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex)); + EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex, false)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); } @@ -2163,7 +2163,7 @@ class Core { new NotificationCompat.Builder(context, channel) .setSmallIcon(R.drawable.baseline_warning_white_24) .setContentTitle(context.getString(R.string.title_notification_failed, title)) - .setContentText(Helper.formatThrowable(ex)) + .setContentText(Helper.formatThrowable(ex, false)) .setContentIntent(pi) .setAutoCancel(false) .setShowWhen(true) @@ -2173,7 +2173,7 @@ class Core { .setVisibility(NotificationCompat.VISIBILITY_SECRET); builder.setStyle(new NotificationCompat.BigTextStyle() - .bigText(Helper.formatThrowable(ex, "\n"))); + .bigText(Helper.formatThrowable(ex, "\n", false))); return builder; } diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index cbba187ea3..50187b88b6 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -1073,7 +1073,7 @@ public class FragmentAccount extends FragmentBase { } private void showError(Throwable ex) { - tvError.setText(Helper.formatThrowable(ex)); + tvError.setText(Helper.formatThrowable(ex, false)); tvError.setVisibility(View.VISIBLE); final View target; diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index c53706607b..f98dee5bab 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -803,7 +803,7 @@ public class FragmentIdentity extends FragmentBase { } private void showError(Throwable ex) { - tvError.setText(Helper.formatThrowable(ex)); + tvError.setText(Helper.formatThrowable(ex, false)); tvError.setVisibility(View.VISIBLE); final View target; diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 821a1622f5..52a53b1cd0 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -2887,7 +2887,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); else new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(Helper.formatThrowable(ex)) + .setMessage(Helper.formatThrowable(ex, false)) .setPositiveButton(android.R.string.cancel, null) .create() .show(); diff --git a/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java b/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java index 2356d92052..d34e1fa032 100644 --- a/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java @@ -386,7 +386,7 @@ public class FragmentQuickSetup extends FragmentBase { if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException) Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); else { - tvError.setText(Helper.formatThrowable(ex)); + tvError.setText(Helper.formatThrowable(ex, false)); tvError.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 719c8ec0f8..71bf4f41cf 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -382,27 +382,33 @@ public class Helper { } static String formatThrowable(Throwable ex) { - return formatThrowable(ex, " "); + return formatThrowable(ex, true); } - static String formatThrowable(Throwable ex, String separator) { - if (ex instanceof MessageRemovedException) - return null; + static String formatThrowable(Throwable ex, boolean santize) { + return formatThrowable(ex, " ", santize); + } - if (ex instanceof IOException && - ex.getCause() instanceof MessageRemovedException) - return null; + static String formatThrowable(Throwable ex, String separator, boolean sanitize) { + if (sanitize) { + if (ex instanceof MessageRemovedException) + return null; - if (ex instanceof FolderClosedException) - return null; + if (ex instanceof IOException && + ex.getCause() instanceof MessageRemovedException) + return null; - if (ex instanceof IllegalStateException && - ("Not connected".equals(ex.getMessage()) || - "This operation is not allowed on a closed folder".equals(ex.getMessage()))) - return null; + if (ex instanceof FolderClosedException) + return null; - if (ex instanceof Core.AlertException) - return ex.getMessage(); + if (ex instanceof IllegalStateException && + ("Not connected".equals(ex.getMessage()) || + "This operation is not allowed on a closed folder".equals(ex.getMessage()))) + return null; + + if (ex instanceof Core.AlertException) + return ex.getMessage(); + } StringBuilder sb = new StringBuilder(); if (BuildConfig.DEBUG) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index adbb77603e..e526dcaa58 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -806,7 +806,7 @@ public class MessageHelper { throw ex; } catch (Throwable ex) { Log.w(ex); - warnings.add(Helper.formatThrowable(ex)); + warnings.add(Helper.formatThrowable(ex, false)); return null; } @@ -828,7 +828,7 @@ public class MessageHelper { } } catch (ParseException ex) { Log.w(ex); - warnings.add(Helper.formatThrowable(ex)); + warnings.add(Helper.formatThrowable(ex, false)); } result = result.replace("\0", ""); @@ -932,7 +932,7 @@ public class MessageHelper { // https://javaee.github.io/javamail/FAQ#imapserverbug if ("Unable to load BODYSTRUCTURE".equals(ex.getMessage())) { Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); try { cmessage = new MimeMessage(imessage); } catch (MessagingException ignored) { @@ -970,7 +970,7 @@ public class MessageHelper { // Nested body: try to continue // ParseException: In parameter list boundary="...">, expected parameter name, got ";" Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); } } else { // https://www.iana.org/assignments/cont-disp/cont-disp.xhtml @@ -981,7 +981,7 @@ public class MessageHelper { disposition = disposition.toLowerCase(); } catch (MessagingException ex) { Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); disposition = null; } @@ -990,7 +990,7 @@ public class MessageHelper { filename = part.getFileName(); } catch (MessagingException ex) { Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); filename = null; } @@ -1018,7 +1018,7 @@ public class MessageHelper { ct = new ContentType(apart.part.getContentType()); } catch (ParseException ex) { Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); ct = new ContentType("application/octet-stream"); } @@ -1027,7 +1027,7 @@ public class MessageHelper { cid = apart.part.getHeader("Content-ID"); } catch (MessagingException ex) { Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); } apart.attachment = new EntityAttachment(); @@ -1072,7 +1072,7 @@ public class MessageHelper { throw ex; } catch (MessagingException ex) { Log.w(ex); - parts.warnings.add(Helper.formatThrowable(ex)); + parts.warnings.add(Helper.formatThrowable(ex, false)); } } diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index c08bbe4153..2218edc859 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -153,7 +153,7 @@ public class ServiceSend extends LifecycleService { Log.e(outbox.name, ex); EntityLog.log( ServiceSend.this, - outbox.name + " " + Helper.formatThrowable(ex)); + outbox.name + " " + Helper.formatThrowable(ex, false)); db.operation().setOperationError(op.id, Helper.formatThrowable(ex)); if (message != null) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 2cce3a1b44..56573a9972 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -600,7 +600,7 @@ public class ServiceSynchronize extends LifecycleService { Log.w(account.name + " alert: " + message); EntityLog.log( ServiceSynchronize.this, account.name + " " + - Helper.formatThrowable(new Core.AlertException(message))); + Helper.formatThrowable(new Core.AlertException(message), false)); db.account().setAccountError(account.id, message); if (message != null && !message.startsWith("Too many simultaneous connections")) { @@ -855,7 +855,7 @@ public class ServiceSynchronize extends LifecycleService { Log.e(folder.name, ex); EntityLog.log( ServiceSynchronize.this, - folder.name + " " + Helper.formatThrowable(ex)); + folder.name + " " + Helper.formatThrowable(ex, false)); state.error(ex); } finally { wlMessage.release(); @@ -888,7 +888,7 @@ public class ServiceSynchronize extends LifecycleService { Log.e(folder.name, ex); EntityLog.log( ServiceSynchronize.this, - folder.name + " " + Helper.formatThrowable(ex)); + folder.name + " " + Helper.formatThrowable(ex, false)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); state.error(ex); } finally { @@ -944,7 +944,7 @@ public class ServiceSynchronize extends LifecycleService { Log.e(folder.name, ex); EntityLog.log( ServiceSynchronize.this, - folder.name + " " + Helper.formatThrowable(ex)); + folder.name + " " + Helper.formatThrowable(ex, false)); state.error(ex); } finally { wlMessage.release(); @@ -966,7 +966,7 @@ public class ServiceSynchronize extends LifecycleService { Log.e(folder.name, ex); EntityLog.log( ServiceSynchronize.this, - folder.name + " " + Helper.formatThrowable(ex)); + folder.name + " " + Helper.formatThrowable(ex, false)); state.error(new FolderClosedException(ifolder, "IDLE")); } finally { Log.i(folder.name + " end idle"); @@ -1047,7 +1047,7 @@ public class ServiceSynchronize extends LifecycleService { Log.e(folder.name, ex); EntityLog.log( ServiceSynchronize.this, - folder.name + " " + Helper.formatThrowable(ex)); + folder.name + " " + Helper.formatThrowable(ex, false)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); state.error(ex); } finally { @@ -1162,7 +1162,7 @@ public class ServiceSynchronize extends LifecycleService { Log.e(account.name, ex); EntityLog.log( ServiceSynchronize.this, - account.name + " " + Helper.formatThrowable(ex)); + account.name + " " + Helper.formatThrowable(ex, false)); db.account().setAccountError(account.id, Helper.formatThrowable(ex)); } finally { // Stop watching for operations