Throw illegalargumentexception instead of nullpointerexception for builder errors

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-12-11 02:28:34 +01:00
parent c1ba1f409c
commit 266ee7626c
3 changed files with 10 additions and 3 deletions

View File

@ -30,7 +30,9 @@ public final class Config {
private final List<Peer> peers;
private Config(final Builder builder) {
interfaze = Objects.requireNonNull(builder.interfaze, "An [Interface] section is required");
if (builder.interfaze == null)
throw new IllegalArgumentException("An [Interface] section is required");
interfaze = builder.interfaze;
// Defensively copy to ensure immutability even if the Builder is reused.
peers = Collections.unmodifiableList(new ArrayList<>(builder.peers));
}

View File

@ -44,11 +44,14 @@ public final class Interface {
private final Optional<Integer> mtu;
private Interface(final Builder builder) {
if (builder.keyPair == null)
throw new IllegalArgumentException("Interfaces must have a private key");
// Defensively copy to ensure immutability even if the Builder is reused.
addresses = Collections.unmodifiableSet(new LinkedHashSet<>(builder.addresses));
dnsServers = Collections.unmodifiableSet(new LinkedHashSet<>(builder.dnsServers));
excludedApplications = Collections.unmodifiableSet(new LinkedHashSet<>(builder.excludedApplications));
keyPair = Objects.requireNonNull(builder.keyPair, "Interfaces must have a private key");
keyPair = builder.keyPair;
listenPort = builder.listenPort;
mtu = builder.mtu;
}

View File

@ -35,12 +35,14 @@ public final class Peer {
private final Key publicKey;
private Peer(final Builder builder) {
if (builder.publicKey == null)
throw new IllegalArgumentException("Peers must have a public key");
// Defensively copy to ensure immutability even if the Builder is reused.
allowedIps = Collections.unmodifiableSet(new LinkedHashSet<>(builder.allowedIps));
endpoint = builder.endpoint;
persistentKeepalive = builder.persistentKeepalive;
preSharedKey = builder.preSharedKey;
publicKey = Objects.requireNonNull(builder.publicKey, "Peers must have a public key");
publicKey = builder.publicKey;
}
/**