0.0.9 | Error handling

This commit is contained in:
Patrick
2026-06-03 19:23:46 +02:00
parent e3c173acc4
commit f89c0ba662
2 changed files with 25 additions and 3 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ plugins {
} }
group = 'de.winniepat' group = 'de.winniepat'
version = '0.0.8' version = '0.0.9'
java { java {
@@ -75,6 +75,8 @@ public class LicenseClient {
String licenseKey, String licenseKey,
String serverId String serverId
) { ) {
URI uri = validateUrl(apiUrl);
JsonObject payload = new JsonObject(); JsonObject payload = new JsonObject();
payload.addProperty("plugin", plugin); payload.addProperty("plugin", plugin);
payload.addProperty("licenseKey", licenseKey); payload.addProperty("licenseKey", licenseKey);
@@ -84,7 +86,7 @@ public class LicenseClient {
} }
return HttpRequest.newBuilder() return HttpRequest.newBuilder()
.uri(URI.create(apiUrl)) .uri(uri)
.timeout(Duration.ofSeconds(10)) .timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload))) .POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload)))
@@ -92,7 +94,13 @@ public class LicenseClient {
} }
private static LicenseResult errorResult(Throwable throwable) { private static LicenseResult errorResult(Throwable throwable) {
return new LicenseError(throwable.getMessage()); String msg = throwable.getMessage();
if (msg == null || msg.isBlank()) {
msg = throwable.getClass().getSimpleName();
}
return new LicenseError(msg);
} }
private static String getString(JsonObject json, String key, String def) { private static String getString(JsonObject json, String key, String def) {
@@ -121,4 +129,18 @@ public class LicenseClient {
getString(json, "checkedAt", null) getString(json, "checkedAt", null)
); );
} }
private static URI validateUrl(String apiUrl) {
URI uri = URI.create(apiUrl);
if (uri.getScheme() == null || (!uri.getScheme().equals("http") && !uri.getScheme().equals("https"))) {
throw new IllegalArgumentException("Invalid URL scheme: " + apiUrl);
}
if (uri.getHost() == null) {
throw new IllegalArgumentException("Invalid URL (missing host): " + apiUrl);
}
return uri;
}
} }