From 694577b9b45f3a3042f65a0e50e278b13dc20a39 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 16 May 2018 17:30:59 +0200 Subject: [PATCH] ExceptionLoggers: never have a null message Signed-off-by: Jason A. Donenfeld --- .../com/wireguard/android/QuickTileService.java | 2 +- .../android/fragment/TunnelController.java | 2 +- .../android/fragment/TunnelEditorFragment.java | 8 ++++---- .../android/fragment/TunnelListFragment.java | 4 ++-- .../android/preference/ZipExporterPreference.java | 2 +- .../wireguard/android/util/ExceptionLoggers.java | 13 +++++++++++-- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/QuickTileService.java b/app/src/main/java/com/wireguard/android/QuickTileService.java index 3bf4d90e..8480b65e 100644 --- a/app/src/main/java/com/wireguard/android/QuickTileService.java +++ b/app/src/main/java/com/wireguard/android/QuickTileService.java @@ -76,7 +76,7 @@ public class QuickTileService extends TileService { final Throwable throwable) { if (throwable == null) return; - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); final String message = getString(R.string.toggle_error, error); Log.e(TAG, message, throwable); Toast.makeText(this, message, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelController.java b/app/src/main/java/com/wireguard/android/fragment/TunnelController.java index 86cf7fa1..aeac6864 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelController.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelController.java @@ -48,7 +48,7 @@ public final class TunnelController { if (throwable == null) return; final Context context = view.getContext(); - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); final int messageResId = checked ? R.string.error_up : R.string.error_down; final String message = context.getString(messageResId, error); Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java index b7ce8820..1dc3f907 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java @@ -54,7 +54,7 @@ public class TunnelEditorFragment extends BaseFragment { Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show(); onFinished(); } else { - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); message = getString(R.string.config_save_error, savedTunnel.getName(), error); Log.e(TAG, message, throwable); if (binding != null) { @@ -119,7 +119,7 @@ public class TunnelEditorFragment extends BaseFragment { try { binding.getConfig().commitData(newConfig); } catch (final Exception e) { - final String error = ExceptionLoggers.unwrap(e).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(e); final String tunnelName = tunnel == null ? binding.getConfig().getName() : tunnel.getName(); final String message = getString(R.string.config_save_error, tunnelName, error); Log.e(TAG, message, e); @@ -171,7 +171,7 @@ public class TunnelEditorFragment extends BaseFragment { Log.d(TAG, message); onFinished(); } else { - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); message = getString(R.string.tunnel_create_error, error); Log.e(TAG, message, throwable); if (binding != null) { @@ -190,7 +190,7 @@ public class TunnelEditorFragment extends BaseFragment { Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel.getName()); renamedTunnel.setConfig(newConfig).whenComplete((a, b) -> onConfigSaved(renamedTunnel, b)); } else { - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); message = getString(R.string.tunnel_rename_error, error); Log.e(TAG, message, throwable); if (binding != null) { diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java index aae12c5f..1138796d 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java @@ -239,7 +239,7 @@ public class TunnelListFragment extends BaseFragment { if (throwable == null) { message = getResources().getQuantityString(R.plurals.delete_success, count, count); } else { - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); message = getResources().getQuantityString(R.plurals.delete_error, count, count, error); Log.e(TAG, message, throwable); } @@ -252,7 +252,7 @@ public class TunnelListFragment extends BaseFragment { String message = null; for (final Throwable throwable : throwables) { - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); message = getString(R.string.import_error, error); Log.e(TAG, message, throwable); } diff --git a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java index 3c065d63..8013279c 100644 --- a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java @@ -102,7 +102,7 @@ public class ZipExporterPreference extends Preference { private void exportZipComplete(final String filePath, final Throwable throwable) { if (throwable != null) { - final String error = ExceptionLoggers.unwrap(throwable).getMessage(); + final String error = ExceptionLoggers.unwrapMessage(throwable); final String message = getContext().getString(R.string.export_error, error); Log.e(TAG, message, throwable); Snackbar.make( diff --git a/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java b/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java index dbc41ff1..cc7cf17e 100644 --- a/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java +++ b/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java @@ -6,8 +6,8 @@ package com.wireguard.android.util; +import android.support.annotation.NonNull; import android.util.Log; - import java9.util.concurrent.CompletionException; import java9.util.function.BiConsumer; @@ -29,11 +29,20 @@ public enum ExceptionLoggers implements BiConsumer { } public static Throwable unwrap(final Throwable throwable) { - if (throwable instanceof CompletionException) + if (throwable instanceof CompletionException && throwable.getCause() != null) return throwable.getCause(); return throwable; } + @NonNull + public static String unwrapMessage(Throwable throwable) { + throwable = unwrap(throwable); + final String message = throwable.getMessage(); + if (message != null) + return message; + return throwable.getClass().getSimpleName(); + } + @Override public void accept(final Object result, final Throwable throwable) { if (throwable != null)