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

Exception handling with @ServerExceptionMapper does not match method with more general exception #41479

Closed
pengxo opened this issue Jun 26, 2024 · 4 comments
Labels
area/rest kind/question Further information is requested

Comments

@pengxo
Copy link

pengxo commented Jun 26, 2024

Describe the bug

Consider the quarkus app with some REST resouces and the following global Exception handling class:

class ExceptionHandler{
    @ServerExceptionMapper
    fun throwable(throwable: Throwable): Response {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("").build()
    }
}

if a POST request is executed with malformed request body that leads to com.fasterxml.jackson.databind.exc.MismatchedInputException, then the exception will not be matched here. I tried to set the method parameter type from Throwable to any super exception class of MismatchedInputException such as com.fasterxml.jackson.databind.JsonMappingException and com.fasterxml.jackson.databind.DatabindException, none of them works. It mathes only when the type is MismatchedInputException.

In the exception handling class, there can be multiple methods with annotation @ServerExceptionMapper and different exception types to have different handling and have the method with type Throwable as default exception handling. But it seems not work well.

Expected behavior

The method with Throwable type should match any exception type if there is no more specific matching.

Actual behavior

The method with Throwable cannot match more specific exception MismatchedInputException

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

21

Quarkus version or git rev

3.11.2 for quarkus

Build tool (ie. output of mvnw --version or gradlew --version)

gradlew

Additional information

No response

@pengxo pengxo added the kind/bug Something isn't working label Jun 26, 2024
@quarkus-bot
Copy link

quarkus-bot bot commented Jun 27, 2024

/cc @FroMage (resteasy-reactive), @stuartwdouglas (resteasy-reactive)

@geoand geoand added kind/question Further information is requested and removed kind/bug Something isn't working labels Jun 27, 2024
@geoand
Copy link
Contributor

geoand commented Jun 27, 2024

This is actually the expected behavior because Quarkus ships with an exception mapper for MismatchedInputException and the JAX-RS / Jakarta REST spec states that a more specific exception mapper takes precedence over a more general one.

@geoand geoand closed this as completed Jun 27, 2024
@pengxo
Copy link
Author

pengxo commented Jun 27, 2024

Hi @geoand, thanks for your explanation. Now I understand the reason. Then in the app one need to find out all the implementations of ExceptionMapper if customizing the error response is required.

@geoand
Copy link
Contributor

geoand commented Jun 27, 2024

Yup and the DevUI will give you that information :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/rest kind/question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants