Skip to content

Commit

Permalink
fix: Improve handling of runtime errors
Browse files Browse the repository at this point in the history
This includes for example TLS-related errors, as
reported in #194 - hence the test case.
  • Loading branch information
mthmulders committed Apr 13, 2023
1 parent d7fbf8e commit c9dae9e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/it/mulders/mcs/common/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public void ifPresent(final Consumer<T> consumer) {
consumer.accept(this.value);
}

@Override
public void ifPresentOrElse(Consumer<T> successConsumer, Consumer<Throwable> failureConsumer) {
successConsumer.accept(value);
}

@Override
public Throwable cause() {
throw new NoSuchElementException("success: " + this.value);
Expand All @@ -36,6 +41,11 @@ public <U> Result<U> map(final Function<T, U> mapping) {
public void ifPresent(final Consumer<T> consumer) {
}

@Override
public void ifPresentOrElse(Consumer<T> successConsumer, Consumer<Throwable> failureConsumer) {
failureConsumer.accept(cause);
}

@Override
public T value() {
throw new NoSuchElementException("failure: " + this.cause.getLocalizedMessage());
Expand All @@ -46,6 +56,8 @@ public T value() {

void ifPresent(final Consumer<T> consumer);

void ifPresentOrElse(final Consumer<T> successConsumer, final Consumer<Throwable> failureConsumer);

T value();

Throwable cause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public SearchCommandHandler(final OutputPrinter coordinateOutput) {
public void search(final SearchQuery query) {
performSearch(query)
.map(response -> performAdditionalSearch(query, response))
.ifPresent(response -> printResponse(query, response));
.ifPresentOrElse(
response -> printResponse(query, response),
failure -> { throw new RuntimeException(failure); }
);
}

private SearchResponse.Response performAdditionalSearch(final SearchQuery query,
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/it/mulders/mcs/search/SearchCommandHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import javax.net.ssl.SSLHandshakeException;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
Expand All @@ -35,6 +37,9 @@ class SearchCommandHandlerTest implements WithAssertions {
private final SearchClient searchClient = new SearchClient() {
@Override
public Result<SearchResponse> search(final SearchQuery query) {
if (query.toSolrQuery().contains("tls-error")) {
return new Result.Failure<>(new SSLHandshakeException("PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"));
}
if (query instanceof WildcardSearchQuery) {
return new Result.Success<>(new SearchResponse(null, wildcardResponse));
} else if (query instanceof CoordinateQuery cq && cq.version().isBlank()) {
Expand All @@ -55,6 +60,12 @@ void should_invoke_search_client() {
handler.search(SearchQuery.search("plexus-utils").build());
verify(outputPrinter).print(any(WildcardSearchQuery.class), eq(wildcardResponse), any());
}

@Test
void should_propagate_tls_exception_to_runtime_exception() {
assertThatThrownBy(() -> handler.search(SearchQuery.search("tls-error").build()))
.isInstanceOf(RuntimeException.class);
}
}

@Nested
Expand All @@ -77,5 +88,11 @@ void should_invoke_search_client_with_groupId_and_artifactId_and_version() {
handler.search(SearchQuery.search("org.codehaus.plexus:plexus-utils:3.4.1").build());
verify(outputPrinter).print(any(CoordinateQuery.class), eq(threePartCoordinateResponse), any());
}

@Test
void should_propagate_tls_exception_to_runtime_exception() {
assertThatThrownBy(() -> handler.search(SearchQuery.search("org.codehaus.plexus:tls-error:3.4.1").build()))
.isInstanceOf(RuntimeException.class);
}
}
}

0 comments on commit c9dae9e

Please sign in to comment.