From 99a34a76913b9f53239e71c33c8f085113566b2b Mon Sep 17 00:00:00 2001 From: Tobias Nett Date: Fri, 24 Nov 2023 00:00:17 +0100 Subject: [PATCH] fix: handle unsuccessful requests in JenkinsClient better (#706) A small change to handle unsuccessful requests via the JenkinsClient more gracefully. Currently, a request failing with 4xx or 5xx will print a WARN log message and the whole stacktrace. However, further up in the code we have the Response object and can check for the status code before continuing processing. With this change, we only log a single WARN message now containing the status code. --- .../org/terasology/launcher/repositories/JenkinsClient.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/terasology/launcher/repositories/JenkinsClient.java b/src/main/java/org/terasology/launcher/repositories/JenkinsClient.java index 8c972c07..d7476a20 100644 --- a/src/main/java/org/terasology/launcher/repositories/JenkinsClient.java +++ b/src/main/java/org/terasology/launcher/repositories/JenkinsClient.java @@ -69,7 +69,11 @@ Jenkins.ApiResult request(URL url) throws InterruptedException { var request = new Request.Builder().url(url).build(); try (var response = client.newCall(request).execute()) { logger.debug("{}{}", response, response.cacheResponse() != null ? " (cached)" : ""); - return gson.fromJson(response.body().string(), Jenkins.ApiResult.class); + if (response.isSuccessful()) { + return gson.fromJson(response.body().string(), Jenkins.ApiResult.class); + } else { + logger.warn("Failed to read from URL '{}' with status code {}.", url.toExternalForm(), response.code()); + } } catch (JsonSyntaxException | JsonIOException e) { logger.warn("Failed to read JSON from '{}'", url.toExternalForm(), e); } catch (IOException e) {