Skip to content

Commit

Permalink
fix: handle unsuccessful requests in JenkinsClient better (#706)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
skaldarnar committed Nov 23, 2023
1 parent 0eb85d0 commit 99a34a7
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 99a34a7

Please sign in to comment.