ExceptionLoggers: never have a null message

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-05-16 17:30:59 +02:00
parent 9c3091456d
commit 694577b9b4
6 changed files with 20 additions and 11 deletions

View File

@ -76,7 +76,7 @@ public class QuickTileService extends TileService {
final Throwable throwable) { final Throwable throwable) {
if (throwable == null) if (throwable == null)
return; return;
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrapMessage(throwable);
final String message = getString(R.string.toggle_error, error); final String message = getString(R.string.toggle_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); Toast.makeText(this, message, Toast.LENGTH_LONG).show();

View File

@ -48,7 +48,7 @@ public final class TunnelController {
if (throwable == null) if (throwable == null)
return; return;
final Context context = view.getContext(); 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 int messageResId = checked ? R.string.error_up : R.string.error_down;
final String message = context.getString(messageResId, error); final String message = context.getString(messageResId, error);
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();

View File

@ -54,7 +54,7 @@ public class TunnelEditorFragment extends BaseFragment {
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
onFinished(); onFinished();
} else { } else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrapMessage(throwable);
message = getString(R.string.config_save_error, savedTunnel.getName(), error); message = getString(R.string.config_save_error, savedTunnel.getName(), error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
if (binding != null) { if (binding != null) {
@ -119,7 +119,7 @@ public class TunnelEditorFragment extends BaseFragment {
try { try {
binding.getConfig().commitData(newConfig); binding.getConfig().commitData(newConfig);
} catch (final Exception e) { } 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 tunnelName = tunnel == null ? binding.getConfig().getName() : tunnel.getName();
final String message = getString(R.string.config_save_error, tunnelName, error); final String message = getString(R.string.config_save_error, tunnelName, error);
Log.e(TAG, message, e); Log.e(TAG, message, e);
@ -171,7 +171,7 @@ public class TunnelEditorFragment extends BaseFragment {
Log.d(TAG, message); Log.d(TAG, message);
onFinished(); onFinished();
} else { } else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrapMessage(throwable);
message = getString(R.string.tunnel_create_error, error); message = getString(R.string.tunnel_create_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
if (binding != null) { if (binding != null) {
@ -190,7 +190,7 @@ public class TunnelEditorFragment extends BaseFragment {
Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel.getName()); Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel.getName());
renamedTunnel.setConfig(newConfig).whenComplete((a, b) -> onConfigSaved(renamedTunnel, b)); renamedTunnel.setConfig(newConfig).whenComplete((a, b) -> onConfigSaved(renamedTunnel, b));
} else { } else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrapMessage(throwable);
message = getString(R.string.tunnel_rename_error, error); message = getString(R.string.tunnel_rename_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
if (binding != null) { if (binding != null) {

View File

@ -239,7 +239,7 @@ public class TunnelListFragment extends BaseFragment {
if (throwable == null) { if (throwable == null) {
message = getResources().getQuantityString(R.plurals.delete_success, count, count); message = getResources().getQuantityString(R.plurals.delete_success, count, count);
} else { } else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrapMessage(throwable);
message = getResources().getQuantityString(R.plurals.delete_error, count, count, error); message = getResources().getQuantityString(R.plurals.delete_error, count, count, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
} }
@ -252,7 +252,7 @@ public class TunnelListFragment extends BaseFragment {
String message = null; String message = null;
for (final Throwable throwable : throwables) { 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); message = getString(R.string.import_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
} }

View File

@ -102,7 +102,7 @@ public class ZipExporterPreference extends Preference {
private void exportZipComplete(final String filePath, final Throwable throwable) { private void exportZipComplete(final String filePath, final Throwable throwable) {
if (throwable != null) { 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); final String message = getContext().getString(R.string.export_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
Snackbar.make( Snackbar.make(

View File

@ -6,8 +6,8 @@
package com.wireguard.android.util; package com.wireguard.android.util;
import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import java9.util.concurrent.CompletionException; import java9.util.concurrent.CompletionException;
import java9.util.function.BiConsumer; import java9.util.function.BiConsumer;
@ -29,11 +29,20 @@ public enum ExceptionLoggers implements BiConsumer<Object, Throwable> {
} }
public static Throwable unwrap(final Throwable throwable) { public static Throwable unwrap(final Throwable throwable) {
if (throwable instanceof CompletionException) if (throwable instanceof CompletionException && throwable.getCause() != null)
return throwable.getCause(); return throwable.getCause();
return throwable; 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 @Override
public void accept(final Object result, final Throwable throwable) { public void accept(final Object result, final Throwable throwable) {
if (throwable != null) if (throwable != null)