Provide semantically meaningful exceptions for translation

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland
2018-12-15 14:46:23 -06:00
parent 3497882ea6
commit dcb0e9b3e8
18 changed files with 364 additions and 186 deletions
@@ -9,8 +9,8 @@ import android.content.Context;
import android.util.Log;
import com.wireguard.android.R;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.Config;
import com.wireguard.config.ParseException;
import java.io.File;
import java.io.FileInputStream;
@@ -69,7 +69,7 @@ public final class FileConfigStore implements ConfigStore {
}
@Override
public Config load(final String name) throws IOException, ParseException {
public Config load(final String name) throws BadConfigException, IOException {
try (final FileInputStream stream = new FileInputStream(fileFor(name))) {
return Config.parse(stream);
}
@@ -18,8 +18,8 @@ import android.view.inputmethod.InputMethodManager;
import com.wireguard.android.Application;
import com.wireguard.android.R;
import com.wireguard.android.databinding.ConfigNamingDialogFragmentBinding;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.Config;
import com.wireguard.config.ParseException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -67,8 +67,8 @@ public class ConfigNamingDialogFragment extends DialogFragment {
try {
config = Config.parse(new ByteArrayInputStream(getArguments().getString(KEY_CONFIG_TEXT).getBytes(StandardCharsets.UTF_8)));
} catch (final IOException | ParseException exception) {
throw new RuntimeException(getResources().getString(R.string.invalid_config_error, getClass().getSimpleName()), exception);
} catch (final BadConfigException | IOException e) {
throw new RuntimeException(getResources().getString(R.string.invalid_config_error, getClass().getSimpleName()), e);
}
}
@@ -40,8 +40,8 @@ import com.wireguard.android.model.Tunnel;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.android.widget.MultiselectableRelativeLayout;
import com.wireguard.android.widget.fab.FloatingActionsMenuRecyclerViewScrollListener;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.Config;
import com.wireguard.config.ParseException;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
@@ -89,8 +89,8 @@ public class TunnelListFragment extends BaseFragment {
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null)
ConfigNamingDialogFragment.newInstance(configText).show(fragmentManager, null);
} catch (final IllegalArgumentException | IOException | ParseException exception) {
onTunnelImportFinished(Collections.emptyList(), Collections.singletonList(exception));
} catch (final BadConfigException | IOException e) {
onTunnelImportFinished(Collections.emptyList(), Collections.singletonList(e));
}
}
@@ -11,8 +11,9 @@ import android.util.Log;
import com.wireguard.android.Application;
import com.wireguard.android.R;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.ParseException;
import com.wireguard.crypto.Key;
import com.wireguard.crypto.KeyFormatException;
import java9.util.concurrent.CompletionException;
import java9.util.function.BiConsumer;
@@ -37,6 +38,8 @@ public enum ExceptionLoggers implements BiConsumer<Object, Throwable> {
public static Throwable unwrap(final Throwable throwable) {
if (throwable instanceof CompletionException && throwable.getCause() != null)
return throwable.getCause();
if (throwable instanceof ParseException && throwable.getCause() != null)
return throwable.getCause();
return throwable;
}
@@ -44,13 +47,14 @@ public enum ExceptionLoggers implements BiConsumer<Object, Throwable> {
final Throwable innerThrowable = unwrap(throwable);
final Resources resources = Application.get().getResources();
String message;
if (innerThrowable instanceof ParseException) {
final ParseException parseException = (ParseException) innerThrowable;
message = resources.getString(R.string.parse_error, parseException.getText(), parseException.getContext());
if (parseException.getMessage() != null)
message += ": " + parseException.getMessage();
} else if (innerThrowable instanceof Key.KeyFormatException) {
final Key.KeyFormatException keyFormatException = (Key.KeyFormatException) innerThrowable;
if (innerThrowable instanceof BadConfigException) {
final BadConfigException configException = (BadConfigException) innerThrowable;
message = resources.getString(R.string.parse_error, configException.getText(), configException.getLocation());
final Throwable cause = unwrap(configException);
if (cause.getMessage() != null)
message += ": " + cause.getMessage();
} else if (innerThrowable instanceof KeyFormatException) {
final KeyFormatException keyFormatException = (KeyFormatException) innerThrowable;
switch (keyFormatException.getFormat()) {
case BASE64:
message = resources.getString(R.string.key_length_base64_exception_message);
@@ -10,8 +10,8 @@ import android.databinding.ObservableList;
import android.os.Parcel;
import android.os.Parcelable;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.Config;
import com.wireguard.config.ParseException;
import com.wireguard.config.Peer;
import java.util.ArrayList;
@@ -63,7 +63,7 @@ public class ConfigProxy implements Parcelable {
return peers;
}
public Config resolve() throws ParseException {
public Config resolve() throws BadConfigException {
final Collection<Peer> resolvedPeers = new ArrayList<>();
for (final PeerProxy proxy : peers)
resolvedPeers.add(proxy.resolve());
@@ -14,9 +14,10 @@ import android.os.Parcelable;
import com.wireguard.android.BR;
import com.wireguard.config.Attribute;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.Interface;
import com.wireguard.config.ParseException;
import com.wireguard.crypto.Key;
import com.wireguard.crypto.KeyFormatException;
import com.wireguard.crypto.KeyPair;
import java.net.InetAddress;
@@ -116,7 +117,7 @@ public class InterfaceProxy extends BaseObservable implements Parcelable {
return publicKey;
}
public Interface resolve() throws ParseException {
public Interface resolve() throws BadConfigException {
final Interface.Builder builder = new Interface.Builder();
if (!addresses.isEmpty())
builder.parseAddresses(addresses);
@@ -157,7 +158,7 @@ public class InterfaceProxy extends BaseObservable implements Parcelable {
this.privateKey = privateKey;
try {
publicKey = new KeyPair(Key.fromBase64(privateKey)).getPublicKey().toBase64();
} catch (final Key.KeyFormatException ignored) {
} catch (final KeyFormatException ignored) {
publicKey = "";
}
notifyPropertyChanged(BR.privateKey);
@@ -15,8 +15,8 @@ import android.support.annotation.Nullable;
import com.wireguard.android.BR;
import com.wireguard.config.Attribute;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.InetEndpoint;
import com.wireguard.config.ParseException;
import com.wireguard.config.Peer;
import com.wireguard.crypto.Key;
@@ -164,7 +164,7 @@ public class PeerProxy extends BaseObservable implements Parcelable {
return allowedIpsState == AllowedIpsState.CONTAINS_IPV4_PUBLIC_NETWORKS;
}
public Peer resolve() throws ParseException {
public Peer resolve() throws BadConfigException {
final Peer.Builder builder = new Peer.Builder();
if (!allowedIps.isEmpty())
builder.parseAllowedIPs(allowedIps);