Skip to content

Commit 82d7ca3

Browse files
fix(java): name the cause of a connection that fails without a message
1 parent ab103a7 commit 82d7ca3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

  • java/api/src

‎java/api/src/main/java/io/github/adeniyikayodee/ngpostcode/api/Client.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ private static String encode(String text) {
146146
}
147147

148148
private static <T> Result<T> failed(Throwable error) {
149-
String reason = error.getMessage() != null ? error.getMessage() : error.getClass().getSimpleName();
149+
String reason = error.getMessage();
150+
if (reason == null) {
151+
// The JDK gives a failed connection no message: its cause says which kind.
152+
Throwable cause = error.getCause();
153+
reason = error.getClass().getSimpleName()
154+
+ (cause != null ? ": " + cause.getClass().getSimpleName() : "");
155+
}
150156
return new Result.Failed<>(new TransportError(reason));
151157
}
152158
}

‎java/api/src/test/java/io/github/adeniyikayodee/ngpostcode/api/ClientTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,12 @@ void aSilentServerTimesOutAsAValueWithoutTheKey() throws IOException {
170170
assertInstanceOf(TransportError.class, nowhere.failure());
171171
}
172172
}
173+
174+
@Test
175+
void aRefusedConnectionSaysWhatFailed() throws IOException {
176+
Client client = clientFor(exchange -> {});
177+
server.stop(0);
178+
var failed = assertInstanceOf(Result.Failed.class, client.send(Api.lookup(CODE)));
179+
assertTrue(failed.failure().toString().matches("ConnectException: \\w+"), failed.failure().toString());
180+
}
173181
}

0 commit comments

Comments
 (0)