Skip to content

Commit

Permalink
improve error msg obscure when connect url port miss
Browse files Browse the repository at this point in the history
  • Loading branch information
camilesing committed Sep 13, 2021
1 parent 920aa06 commit 0aeadb7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/influxdb/InfluxDBFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import okhttp3.OkHttpClient;
import org.influxdb.impl.Preconditions;

import java.net.URL;
import java.net.MalformedURLException;
import java.util.Objects;


Expand All @@ -27,6 +29,7 @@ public enum InfluxDBFactory {
*/
public static InfluxDB connect(final String url) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkHasPort(url);
return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder());
}

Expand All @@ -44,6 +47,7 @@ public static InfluxDB connect(final String url) {
*/
public static InfluxDB connect(final String url, final String username, final String password) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkHasPort(url);
Preconditions.checkNonEmptyString(username, "username");
return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder());
}
Expand All @@ -59,6 +63,7 @@ public static InfluxDB connect(final String url, final String username, final St
*/
public static InfluxDB connect(final String url, final OkHttpClient.Builder client) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkHasPort(url);
Objects.requireNonNull(client, "client");
return new InfluxDBImpl(url, null, null, client);
}
Expand All @@ -79,6 +84,7 @@ public static InfluxDB connect(final String url, final OkHttpClient.Builder clie
*/
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client) {
Preconditions.checkHasPort(url);
return connect(url, username, password, client, ResponseFormat.JSON);
}

Expand All @@ -101,8 +107,10 @@ public static InfluxDB connect(final String url, final String username, final St
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client, final ResponseFormat responseFormat) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkHasPort(url);
Preconditions.checkNonEmptyString(username, "username");
Objects.requireNonNull(client, "client");
return new InfluxDBImpl(url, username, password, client, responseFormat);
}

}
23 changes: 23 additions & 0 deletions src/main/java/org/influxdb/impl/Preconditions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.influxdb.impl;

import java.net.URL;
import java.net.MalformedURLException;

/**
* Functions for parameter validation.
*
Expand Down Expand Up @@ -59,4 +62,24 @@ public static void checkDuration(final String duration, final String name) throw
+ " for " + name);
}
}

/**
* Check url is legal
* @param url the server url
* @throws IllegalArgumentException if the port of url is miss
*/
public static void checkHasPort(final String url) throws IllegalArgumentException{
String colon = ":";
if (!url.contains(colon) || url.endsWith(colon)) {
throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url));
}
try {
URL urlObj = new URL(url);
if (-1 == urlObj.getPort()) {
throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url));
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
12 changes: 12 additions & 0 deletions src/test/java/org/influxdb/InfluxDBFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ public void testShouldThrowIllegalArgumentWithInvalidUrl() {
InfluxDBFactory.connect("invalidUrl");
});
}

@Test
public void testUrlNotContainsColon() {
Assertions.assertThrows(IllegalArgumentException.class, () ->
InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP()));
}

@Test
public void testUrlEndWithColon() {
Assertions.assertThrows(IllegalArgumentException.class, () ->
InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":"));
}
}

0 comments on commit 0aeadb7

Please sign in to comment.