Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error msg obscure when connect url port miss #769

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() + ":"));
}
}