diff --git a/src/main/java/de/malkusch/km200/http/Http.java b/src/main/java/de/malkusch/km200/http/Http.java index 6659b4f..11e3d8c 100644 --- a/src/main/java/de/malkusch/km200/http/Http.java +++ b/src/main/java/de/malkusch/km200/http/Http.java @@ -41,19 +41,15 @@ public final Response post(String path, byte[] body) throws KM200Exception, IOEx protected abstract Response exchange(Request request) throws IOException, InterruptedException, KM200Exception; static Response assertHttpOk(Request request, Response response) throws KM200Exception { - var status = response.status(); - if (status >= 200 && status <= 299) { - return response; - - } else { - throw switch (status) { - case 400 -> new KM200Exception.BadRequest(request + " was a bad request"); - case 403 -> new KM200Exception.Forbidden(request + " is forbidden"); - case 404 -> new KM200Exception.NotFound(request + " was not found"); - case 423 -> new KM200Exception.Locked(request + " was locked"); - case 500 -> new KM200Exception.ServerError(request + " resulted in a server error"); - default -> new KM200Exception(request + " failed with response code " + status); - }; - } + return switch ((Integer) response.status()) { + case Integer status when (status >= 200 && status <= 299) -> response; + + case 400 -> throw new KM200Exception.BadRequest(request + " was a bad request"); + case 403 -> throw new KM200Exception.Forbidden(request + " is forbidden"); + case 404 -> throw new KM200Exception.NotFound(request + " was not found"); + case 423 -> throw new KM200Exception.Locked(request + " was locked"); + case 500 -> throw new KM200Exception.ServerError(request + " resulted in a server error"); + default -> throw new KM200Exception(request + " failed with response code " + response.status()); + }; } } diff --git a/src/main/java/de/malkusch/km200/http/RetryHttp.java b/src/main/java/de/malkusch/km200/http/RetryHttp.java index 56c36f2..b95695f 100644 --- a/src/main/java/de/malkusch/km200/http/RetryHttp.java +++ b/src/main/java/de/malkusch/km200/http/RetryHttp.java @@ -38,7 +38,8 @@ public Response exchange(Request request) throws IOException, InterruptedExcepti case IOException cause -> throw cause; case InterruptedException cause -> throw cause; case KM200Exception cause -> throw cause; - default -> throw new KM200Exception("Unexpected retry error for " + request.path(), e); + case Throwable cause -> throw new KM200Exception("Unexpected retry error for " + request.path(), cause); + case null -> throw new KM200Exception("Unexpected retry error for " + request.path(), e); } } } diff --git a/src/main/java/de/malkusch/km200/http/UrlHttp.java b/src/main/java/de/malkusch/km200/http/UrlHttp.java index 65654d9..fe445fe 100644 --- a/src/main/java/de/malkusch/km200/http/UrlHttp.java +++ b/src/main/java/de/malkusch/km200/http/UrlHttp.java @@ -1,6 +1,7 @@ package de.malkusch.km200.http; import java.io.IOException; +import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.SocketTimeoutException; @@ -45,9 +46,11 @@ protected Response exchange(Request request) throws IOException, InterruptedExce throw new IOException(request + " received invalid HTTP response"); } - try (var input = connection.getErrorStream() != null ? connection.getErrorStream() - : connection.getInputStream()) { - + var input = switch (connection.getErrorStream()) { + case InputStream error -> error; + case null -> connection.getInputStream(); + }; + try (input) { var body = input.readAllBytes(); var response = new Response(status, body); return assertHttpOk(request, response);