summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2018-08-01 02:47:34 +0530
committerJason A. Donenfeld <Jason@zx2c4.com>2018-08-26 11:04:01 -0600
commitc38f6c471d640a73e9c512b64c22001cdec3ba65 (patch)
tree404fc99e6684998f39a6466b4581decbba3c4f4b /app/src/main/java/com/wireguard/config
parent85462de254050cc693f6f51cc549899fd656913a (diff)
Extract error messages to string resources
Useful for validation errors and localisation later on Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/wireguard/config')
-rw-r--r--app/src/main/java/com/wireguard/config/Config.java8
-rw-r--r--app/src/main/java/com/wireguard/config/InetAddresses.java5
-rw-r--r--app/src/main/java/com/wireguard/config/Interface.java8
-rw-r--r--app/src/main/java/com/wireguard/config/Peer.java10
4 files changed, 23 insertions, 8 deletions
diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java
index db8c2fc7..fbfc35fc 100644
--- a/app/src/main/java/com/wireguard/config/Config.java
+++ b/app/src/main/java/com/wireguard/config/Config.java
@@ -6,6 +6,7 @@
package com.wireguard.config;
+import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
@@ -15,6 +16,8 @@ import android.os.Parcelable;
import android.support.annotation.Nullable;
import com.android.databinding.library.baseAdapters.BR;
+import com.wireguard.android.Application;
+import com.wireguard.android.R;
import java.io.BufferedReader;
import java.io.IOException;
@@ -43,6 +46,7 @@ public class Config {
public static Config from(final BufferedReader reader) throws IOException {
final Config config = new Config();
+ final Context context = Application.get();
Peer currentPeer = null;
String line;
boolean inInterfaceSection = false;
@@ -65,11 +69,11 @@ public class Config {
} else if (currentPeer != null) {
currentPeer.parse(line);
} else {
- throw new IllegalArgumentException("Invalid configuration line: " + line);
+ throw new IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_config_line, line));
}
}
if (!inInterfaceSection && currentPeer == null) {
- throw new IllegalArgumentException("Could not find any config information");
+ throw new IllegalArgumentException(context.getString(R.string.tunnel_error_no_config_information));
}
return config;
}
diff --git a/app/src/main/java/com/wireguard/config/InetAddresses.java b/app/src/main/java/com/wireguard/config/InetAddresses.java
index b7304cce..2cb43343 100644
--- a/app/src/main/java/com/wireguard/config/InetAddresses.java
+++ b/app/src/main/java/com/wireguard/config/InetAddresses.java
@@ -8,6 +8,9 @@ package com.wireguard.config;
import android.support.annotation.Nullable;
+import com.wireguard.android.Application;
+import com.wireguard.android.R;
+
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
@@ -30,7 +33,7 @@ public final class InetAddresses {
public static InetAddress parse(@Nullable final String address) {
if (address == null || address.isEmpty())
- throw new IllegalArgumentException("Empty address");
+ throw new IllegalArgumentException(Application.get().getString(R.string.tunnel_error_empty_inetaddress));
try {
return (InetAddress) PARSER_METHOD.invoke(null, address);
} catch (final IllegalAccessException | InvocationTargetException e) {
diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java
index eea1d18b..c43c2c5b 100644
--- a/app/src/main/java/com/wireguard/config/Interface.java
+++ b/app/src/main/java/com/wireguard/config/Interface.java
@@ -6,13 +6,16 @@
package com.wireguard.config;
+import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
+import com.wireguard.android.Application;
import com.wireguard.android.BR;
+import com.wireguard.android.R;
import com.wireguard.crypto.Keypair;
import java.net.InetAddress;
@@ -31,6 +34,7 @@ public class Interface {
@Nullable private Keypair keypair;
private int listenPort;
private int mtu;
+ private final Context context = Application.get();
public Interface() {
addressList = new ArrayList<>();
@@ -42,7 +46,7 @@ public class Interface {
if (addresses != null && addresses.length > 0) {
for (final String addr : addresses) {
if (addr.isEmpty())
- throw new IllegalArgumentException("Address is empty");
+ throw new IllegalArgumentException(context.getString(R.string.tunnel_error_empty_interface_address));
addressList.add(new InetNetwork(addr));
}
}
@@ -141,7 +145,7 @@ public class Interface {
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == null)
- throw new IllegalArgumentException(String.format("Unable to parse line: \"%s\"", line));
+ throw new IllegalArgumentException(String.format(context.getString(R.string.tunnel_error_interface_parse_failed), line));
switch (key) {
case ADDRESS:
addAddresses(key.parseList(line));
diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java
index b65f52cb..1ed615e6 100644
--- a/app/src/main/java/com/wireguard/config/Peer.java
+++ b/app/src/main/java/com/wireguard/config/Peer.java
@@ -6,6 +6,7 @@
package com.wireguard.config;
+import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.os.Parcel;
@@ -13,6 +14,8 @@ import android.os.Parcelable;
import android.support.annotation.Nullable;
import com.android.databinding.library.baseAdapters.BR;
+import com.wireguard.android.Application;
+import com.wireguard.android.R;
import com.wireguard.crypto.KeyEncoding;
import java.net.Inet6Address;
@@ -38,6 +41,7 @@ public class Peer {
private int persistentKeepalive;
@Nullable private String preSharedKey;
@Nullable private String publicKey;
+ private final Context context = Application.get();
public Peer() {
allowedIPsList = new ArrayList<>();
@@ -117,7 +121,7 @@ public class Peer {
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == null)
- throw new IllegalArgumentException(String.format("Unable to parse line: \"%s\"", line));
+ throw new IllegalArgumentException(context.getString(R.string.tunnel_error_interface_parse_failed, line));
switch (key) {
case ALLOWED_IPS:
addAllowedIPs(key.parseList(line));
@@ -152,7 +156,7 @@ public class Peer {
if (endpoint != null && !endpoint.isEmpty()) {
final InetSocketAddress constructedEndpoint;
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
- throw new IllegalArgumentException("Forbidden characters in endpoint");
+ throw new IllegalArgumentException(context.getString(R.string.tunnel_error_forbidden_endpoint_chars));
final URI uri;
try {
uri = new URI("wg://" + endpoint);
@@ -253,7 +257,7 @@ public class Peer {
parent.setPreSharedKey(preSharedKey);
parent.setPublicKey(publicKey);
if (parent.getPublicKey() == null)
- throw new IllegalArgumentException("Peer public key may not be empty");
+ throw new IllegalArgumentException(Application.get().getString(R.string.tunnel_error_empty_peer_public_key));
loadData(parent);
notifyChange();
}