Skip to content

Commit

Permalink
Remove stacktraces from exceptions thrown deliberately in RESTEasy Re…
Browse files Browse the repository at this point in the history
…active tests

This is done so developers looking at the output of tests will not be tricked
by false negatives
  • Loading branch information
geoand committed Aug 23, 2021
1 parent 504e824 commit 5736e48
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public JavaArchive get() {
.addClasses(FirstResource.class, SecondResource.class,
MyException.class, MyOtherException.class, UniException.class, ExtendsUniException.class,
MyOtherExceptionMapper.class, UniExceptionMapper.class,
SomeBean.class);
SomeBean.class, ExceptionUtil.class);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.resteasy.reactive.server.test.customexceptions;

final class ExceptionUtil {

private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];

private ExceptionUtil() {
}

static <T extends Throwable> T removeStackTrace(T t) {
t.setStackTrace(EMPTY_STACK_TRACE);
return t;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.resteasy.reactive.server.test.customexceptions;

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

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -23,17 +25,17 @@ public class FirstResource {
@Produces("text/plain")
public String throwsVariousExceptions(@RestQuery String name) {
if (name.startsWith("IllegalArgument")) {
throw new IllegalArgumentException();
throw removeStackTrace(new IllegalArgumentException());
} else if (name.startsWith("IllegalState")) {
throw new IllegalStateException("IllegalState");
throw removeStackTrace(new IllegalStateException("IllegalState"));
} else if (name.startsWith("MyOther")) {
throw new MyOtherException();
throw removeStackTrace(new MyOtherException());
} else if (name.startsWith("My")) {
throw new MyException();
throw removeStackTrace(new MyException());
} else if (name.startsWith("Uni")) {
throw new UniException();
throw removeStackTrace(new UniException());
}
throw new RuntimeException();
throw removeStackTrace(new RuntimeException());
}

@GET
Expand All @@ -52,7 +54,7 @@ public Uni<String> uni(@RestQuery String name) {
} else if (name.startsWith("Uni")) {
e = new UniException();
}
return Uni.createFrom().failure(e);
return Uni.createFrom().failure(removeStackTrace(e));
}

@ServerExceptionMapper({ IllegalStateException.class, IllegalArgumentException.class })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.resteasy.reactive.server.test.customexceptions;

import static io.quarkus.resteasy.reactive.server.test.customexceptions.ExceptionUtil.*;

import java.util.function.Supplier;

import javax.ws.rs.GET;
Expand Down Expand Up @@ -40,7 +42,7 @@ public static class Resource {
@Path("throwable")
@Produces("text/plain")
public String throwsThrowable() throws Throwable {
throw new Throwable();
throw removeStackTrace(new Throwable());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.resteasy.reactive.server.test.customexceptions;

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

import java.util.function.Supplier;

import javax.ws.rs.GET;
Expand Down Expand Up @@ -49,7 +51,7 @@ public Response handleThrowable(Throwable t) {
@Path("throwable")
@Produces("text/plain")
public String throwsThrowable() throws Throwable {
throw new Throwable();
throw removeStackTrace(new Throwable());
}
}

Expand All @@ -60,7 +62,7 @@ public static class DoesNotHaveCustomThrowableHandlerResource {
@Path("throwable")
@Produces("text/plain")
public String throwsThrowable() throws Throwable {
throw new Throwable();
throw removeStackTrace(new Throwable());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.resteasy.reactive.server.test.customexceptions;

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

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -10,27 +12,27 @@ public class SecondResource {
@GET
@Produces("text/plain")
public String throwsMyException() {
throw new MyException();
throw removeStackTrace(new MyException());
}

@GET
@Path("other")
@Produces("text/plain")
public String throwsMyOtherException() {
throw new MyOtherException();
throw removeStackTrace(new MyOtherException());
}

@GET
@Path("uni")
@Produces("text/plain")
public String throwsUniException() {
throw new UniException();
throw removeStackTrace(new UniException());
}

@GET
@Path("extendsUni")
@Produces("text/plain")
public String throwsExtendsUniException() {
throw new ExtendsUniException();
throw removeStackTrace(new ExtendsUniException());
}
}

0 comments on commit 5736e48

Please sign in to comment.