Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that connection issues can be handled in Reactive REST Client #24368

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always the first handler invoked? Curious if the response headers could be overwritten if they were set previously

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really matter if it gets overridden. All that matters in this case is that it's not null

requestContext.setResponseReasonPhrase("unknown");

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