-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Introduce @UnwrapException
for Quarkus REST
#42094
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
...t/java/io/quarkus/resteasy/reactive/server/test/customexceptions/UnwrapExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package io.quarkus.resteasy.reactive.server.test.customexceptions; | ||
|
||
import static io.quarkus.resteasy.reactive.server.test.ExceptionUtil.removeStackTrace; | ||
import static io.restassured.RestAssured.when; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
import org.jboss.resteasy.reactive.server.UnwrapException; | ||
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; | ||
|
||
public class UnwrapExceptionTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(FirstException.class, SecondException.class, ThirdException.class, | ||
FourthException.class, FifthException.class, SixthException.class, | ||
Mappers.class, Resource.class, ExceptionUtil.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testWrapperWithUnmappedException() { | ||
when().get("/hello/iaeInSecond") | ||
.then().statusCode(500); | ||
} | ||
|
||
@Test | ||
public void testMappedExceptionWithoutUnwrappedWrapper() { | ||
when().get("/hello/iseInFirst") | ||
.then().statusCode(500); | ||
|
||
when().get("/hello/iseInThird") | ||
.then().statusCode(500); | ||
|
||
when().get("/hello/iseInSixth") | ||
.then().statusCode(500); | ||
} | ||
|
||
@Test | ||
public void testWrapperWithMappedException() { | ||
when().get("/hello/iseInSecond") | ||
.then().statusCode(900); | ||
|
||
when().get("/hello/iseInFourth") | ||
.then().statusCode(900); | ||
|
||
when().get("/hello/iseInFifth") | ||
.then().statusCode(900); | ||
} | ||
|
||
@Path("hello") | ||
public static class Resource { | ||
|
||
@Path("iseInFirst") | ||
public String throwsISEAsCauseOfFirstException() { | ||
throw removeStackTrace(new FirstException(removeStackTrace(new IllegalStateException("dummy")))); | ||
} | ||
|
||
@Path("iseInSecond") | ||
public String throwsISEAsCauseOfSecondException() { | ||
throw removeStackTrace(new SecondException(removeStackTrace(new IllegalStateException("dummy")))); | ||
} | ||
|
||
@Path("iaeInSecond") | ||
public String throwsIAEAsCauseOfSecondException() { | ||
throw removeStackTrace(new SecondException(removeStackTrace(new IllegalArgumentException("dummy")))); | ||
} | ||
|
||
@Path("iseInThird") | ||
public String throwsISEAsCauseOfThirdException() throws ThirdException { | ||
throw removeStackTrace(new ThirdException(removeStackTrace(new IllegalStateException("dummy")))); | ||
} | ||
|
||
@Path("iseInFourth") | ||
public String throwsISEAsCauseOfFourthException() throws FourthException { | ||
throw removeStackTrace(new FourthException(removeStackTrace(new IllegalStateException("dummy")))); | ||
} | ||
|
||
@Path("iseInFifth") | ||
public String throwsISEAsCauseOfFifthException() { | ||
throw removeStackTrace(new FifthException(removeStackTrace(new IllegalStateException("dummy")))); | ||
} | ||
|
||
@Path("iseInSixth") | ||
public String throwsISEAsCauseOfSixthException() { | ||
throw removeStackTrace(new SixthException(removeStackTrace(new IllegalStateException("dummy")))); | ||
} | ||
} | ||
|
||
@UnwrapException({ FourthException.class, FifthException.class }) | ||
public static class Mappers { | ||
|
||
@ServerExceptionMapper | ||
public Response handleIllegalStateException(IllegalStateException e) { | ||
return Response.status(900).build(); | ||
} | ||
} | ||
|
||
public static class FirstException extends RuntimeException { | ||
|
||
public FirstException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
@UnwrapException | ||
public static class SecondException extends FirstException { | ||
|
||
public SecondException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
public static class ThirdException extends Exception { | ||
|
||
public ThirdException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
public static class FourthException extends SecondException { | ||
|
||
public FourthException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
public static class FifthException extends RuntimeException { | ||
|
||
public FifthException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
public static class SixthException extends RuntimeException { | ||
|
||
public SixthException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...tive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/UnwrapException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.jboss.resteasy.reactive.server; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to configure that an exception (or exceptions) should be unwrapped during exception handling. | ||
* <p> | ||
* Unwrapping means that when an {@link Exception} of the configured type is thrown and no | ||
* {@code jakarta.ws.rs.ext.ExceptionMapper} exists, | ||
* then RESTEasy Reactive will attempt to locate an {@code ExceptionMapper} for the cause of the Exception. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface UnwrapException { | ||
|
||
/** | ||
* If this is not set, the value is assumed to be the exception class where the annotation is placed | ||
*/ | ||
Class<? extends Exception>[] value() default {}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit subtle. This unwrapping only happens when there's no registered mapper for the direct exception type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this should be mentioned in the docs? (The guide, not just the javadoc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is implied, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not find it implied, no. How I read it was that the minute we declare that an exception should be unwrapped, it will never match an exceptoin mapper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to push an update with what you have in mind, I'm perfectly fine with that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to phrase this in the docs, especially given that it's already a TIP followed by a NOTE, I feel like this succession of tips/notes should be turned into sub-sections :-/
Also, I'm now realising that it should probably be a warning (or error?) if we have both an exception mapper for
T
and an unwrap exception forT
, no? In this case, the exception will never be unwrapped.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that can be a good follow-up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, very likely so.