From 9909bf3217f171b9831413a1b1be07583677e621 Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Thu, 2 May 2024 11:38:31 +0200 Subject: [PATCH] fix: Improve error logging in case of connection failure (#367) Closes #363. --- src/main/java/it/mulders/mcs/search/SearchClient.java | 4 ++++ src/test/java/it/mulders/mcs/search/SearchClientIT.java | 1 + 2 files changed, 5 insertions(+) diff --git a/src/main/java/it/mulders/mcs/search/SearchClient.java b/src/main/java/it/mulders/mcs/search/SearchClient.java index 3fe2bc4e..a7840081 100644 --- a/src/main/java/it/mulders/mcs/search/SearchClient.java +++ b/src/main/java/it/mulders/mcs/search/SearchClient.java @@ -4,6 +4,7 @@ import it.mulders.mcs.common.SearchResponseBodyHandler; import java.io.IOException; +import java.net.ConnectException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -32,6 +33,9 @@ public Result search(final SearchQuery query) { try { return client.send(request, new SearchResponseBodyHandler()) .body(); + } catch (ConnectException e) { + // The JDK HTTP client throws a ConnectException without a message, we can do better. + return new Result.Failure<>(new ConnectException("Can't resolve " + hostname)); } catch (IOException | InterruptedException e) { return new Result.Failure<>(e); } diff --git a/src/test/java/it/mulders/mcs/search/SearchClientIT.java b/src/test/java/it/mulders/mcs/search/SearchClientIT.java index 5fc58d37..c7b781d3 100644 --- a/src/test/java/it/mulders/mcs/search/SearchClientIT.java +++ b/src/test/java/it/mulders/mcs/search/SearchClientIT.java @@ -129,6 +129,7 @@ void should_gracefully_handle_connection_failure() { assertThat(result).isInstanceOf(Result.Failure.class); assertThat(result.cause()).isInstanceOf(ConnectException.class); + assertThat(result.cause().getLocalizedMessage()).contains("localhost:21"); } } } \ No newline at end of file