HumanReadableException extends RuntimeException { @Override public String toString() { Throwable cause = getCause(); if (cause == null) { return ": " + getMessage(); } List<Throwable> list = stackToList(this, new ArrayList<>()); Optional<HumanReadableException> humanReadableExceptionCandidate = findHumanReadableException(list); if (humanReadableExceptionCandidate.isPresent()) { return humanReadableExceptionCandidate.get().toString(); } if (findConnectionError(list)) { return ": failed to connect to the API"; } Optional<String> messageCandidate = findMessage(list); if (messageCandidate.isPresent()) { return ":" + messageCandidate.get(); } return ": unknown error"; } HumanReadableException(String message);  HumanReadableException(Throwable cause); @Override String toString();  }
@Test public void testConnectionError() { HumanReadableException exception = new HumanReadableException(new RuntimeException(new ConnectException())); assertThat(exception.toString().contains("connect"), is(true)); }
@Test public void testSocketTimeout() { HumanReadableException exception = new HumanReadableException(new SocketTimeoutException()); assertThat(exception.toString().contains("connect"), is(true)); }
@Test public void testMessage() { HumanReadableException exception = new HumanReadableException("test-me"); assertThat(exception.toString().contains("test"), is(true)); }