Skip to content

Commit

Permalink
Ensure that connection issues can be handled in Reactive REST Client
Browse files Browse the repository at this point in the history
Fixes: #24364
  • Loading branch information
geoand committed Mar 17, 2022
1 parent d0b2b43 commit e47508d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.rest.client.reactive;

import java.net.URI;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.assertj.core.api.Assertions;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class InvalidHostTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest();

@Test
void shouldThrowDummyException() {
Client client = RestClientBuilder.newBuilder().baseUri(URI.create("http://localhost2:1234/"))
.register(DummyExceptionMapper.class).build(Client.class);

Assertions.assertThatThrownBy(client::get).isInstanceOf(DummyException.class);
}

@Path("/foo")
public interface Client {
@GET
String get();
}

public static class DummyException extends RuntimeException {

}

public static class DummyExceptionMapper implements ResponseExceptionMapper<DummyException> {

@Override
public boolean handles(int status, MultivaluedMap<String, Object> headers) {
return status == 0 && headers.isEmpty();
}

@Override
public DummyException toThrowable(Response response) {
return new DummyException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.client.spi.MultipartResponseData;
import org.jboss.resteasy.reactive.common.core.Serialisers;
import org.jboss.resteasy.reactive.common.util.MultivaluedTreeMap;

public class ClientSendRequestHandler implements ClientRestHandler {
private static final Logger log = Logger.getLogger(ClientSendRequestHandler.class);
Expand Down Expand Up @@ -247,6 +248,10 @@ public void handle(Throwable failure) {
}, new Consumer<>() {
@Override
public void accept(Throwable event) {
// set some properties to prevent NPEs down the chain
requestContext.setResponseHeaders(new MultivaluedTreeMap<>());
requestContext.setResponseReasonPhrase("unknown");

if (event instanceof IOException) {
ProcessingException throwable = new ProcessingException(event);
reportFinish(throwable, requestContext);
Expand Down

0 comments on commit e47508d

Please sign in to comment.