Skip to content

Commit

Permalink
Merge pull request #36873 from andrejpetras/feat/server-exception-mapper
Browse files Browse the repository at this point in the history
Register server exception mapper method of the Rest-Interface implementation class
  • Loading branch information
geoand authored Nov 7, 2023
2 parents ba3ea6a + 479cae1 commit 46744fe
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.quarkus.resteasy.reactive.server.test.customexceptions;

import static io.quarkus.resteasy.reactive.server.test.ExceptionUtil.removeStackTrace;

import java.util.function.Supplier;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
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.resteasy.reactive.server.test.ExceptionUtil;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class ImplClassExceptionMapperTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(CustomResourceImpl.class,
CustomResource.class,
GlobalCustomResourceImpl.class,
GlobalCustomResource.class,
DefaultCustomResource.class,
ExceptionUtil.class);
}
});

@Test
public void test() {
RestAssured.get("/custom/error")
.then().statusCode(416);
RestAssured.get("/stock/error")
.then().statusCode(500);
RestAssured.get("/default/error")
.then().statusCode(417);
}

@Path("default")
public static class DefaultCustomResource {

@ServerExceptionMapper
public Response handleThrowable(RuntimeException t) {
return Response.status(417).build();
}

@GET
@Path("error")
@Produces("text/plain")
public String throwsException() {
throw removeStackTrace(new RuntimeException());
}
}

public static class CustomResourceImpl implements CustomResource {

@ServerExceptionMapper
public Response handleThrowable(RuntimeException t) {
return Response.status(416).build();
}

public String throwsException() {
throw removeStackTrace(new RuntimeException());
}
}

@Path("custom")
public interface CustomResource {
@GET
@Path("error")
@Produces("text/plain")
String throwsException();
}

public static class GlobalCustomResourceImpl implements GlobalCustomResource {

public String throwsException() {
throw removeStackTrace(new RuntimeException());
}
}

@Path("stock")
public interface GlobalCustomResource {

@GET
@Path("error")
@Produces("text/plain")
String throwsException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ public static ResourceScanningResult scanResources(
if (!scannedResources.containsKey(clazz.name())) {
scannedResources.put(clazz.name(), clazz);
scannedResourcePaths.put(clazz.name(), i.getValue());

// check for server exception mapper method in implementation class of the interface.
List<AnnotationInstance> exceptionMapperAnnotationInstances = clazz.annotationsMap()
.get(ResteasyReactiveDotNames.SERVER_EXCEPTION_MAPPER);
if (exceptionMapperAnnotationInstances != null) {
for (AnnotationInstance instance : exceptionMapperAnnotationInstances) {
if (instance.target().kind() != AnnotationTarget.Kind.METHOD) {
continue;
}
methodExceptionMappers.add(instance.target().asMethod());
}
}
}
}
}
Expand Down

0 comments on commit 46744fe

Please sign in to comment.