Skip to content

Commit

Permalink
fix: Improve error logging in case of connection failure (#367)
Browse files Browse the repository at this point in the history
Closes #363.
  • Loading branch information
mthmulders authored May 2, 2024
1 parent 2be9a5b commit 9909bf3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/it/mulders/mcs/search/SearchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +33,9 @@ public Result<SearchResponse> 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);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/it/mulders/mcs/search/SearchClientIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

0 comments on commit 9909bf3

Please sign in to comment.