Skip to content

Commit

Permalink
Unwrap CompletionException when dealing with CompletionStage in REST …
Browse files Browse the repository at this point in the history
…server

Fixes: quarkusio#41996
  • Loading branch information
geoand authored and holly-cummins committed Jul 31, 2024
1 parent 93ecee6 commit bc05a09
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.quarkus.resteasy.reactive.server.test;

import static org.hamcrest.Matchers.equalTo;

import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.mutiny.Uni;

public class MutinyAsCompletionStageTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(TestResource.class);
}
});

@Test
public void testOk() {
RestAssured.get("/test/ok")
.then()
.statusCode(200)
.body(equalTo("test"));
}

@Test
public void testError() {
RestAssured.get("/test/error")
.then()
.statusCode(400);
}

@Path("test")
public static class TestResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/ok")
public CompletionStage<String> ok() {
return Uni.createFrom().item("test").subscribeAsCompletionStage();
}

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/error")
public CompletionStage<String> error() {
return Uni.createFrom().<String> failure(new BadRequestException()).subscribeAsCompletionStage();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jboss.resteasy.reactive.server.handlers;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -28,7 +29,11 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
}
} else {
if (t != null) {
requestContext.handleException(t, true);
if (t instanceof CompletionException ce) {
requestContext.handleException(ce.getCause(), true);
} else {
requestContext.handleException(t, true);
}
} else {
requestContext.setResult(v);
}
Expand Down

0 comments on commit bc05a09

Please sign in to comment.