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) {
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();

View File

@ -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();

View File

@ -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) {

View File

@ -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);
}

View File

@ -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(

View File

@ -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<Object, Throwable> {
}
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)