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

Support Quarkus / RestEasy ServerExceptionMapper annotation #2032

Open
indiealexh opened this issue Oct 23, 2024 · 0 comments
Open

Support Quarkus / RestEasy ServerExceptionMapper annotation #2032

indiealexh opened this issue Oct 23, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@indiealexh
Copy link

Request to support RestEasy's @ServerExceptionMapper method annotation

This request is an extension of the feature developed in #220 and stems from this quarkus issue: quarkusio/quarkus#44035

smallrye-openapi can scan for @Provider ExceptionMappers and add them to endpoints that throw that exception. The same should be possible for exceptions mapped by using RestEasy's @ServerExceptionMapper method annotation.

Example for illustration

Here is an example of what I would like to do using @ServerExceptionMapper:

@Path("/bundles")
@Produces({MediaType.APPLICATION_JSON})
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Bundles")
public class BundlesResource {
    [...]
    @POST
    @Path("/{bundleId}/actions/render")
    @APIResponses({
            @APIResponse(responseCode = "200", description = "Render bundle"),
    })
    public Uni<BundleRender> renderBundle(@RestPath long bundleId, BundleRenderRequest bundleRenderRequest) throws BundleRenderValidationException {
        return bundleRenderService.renderBundle(bundleId, bundleRenderRequest);
    }

    @ServerExceptionMapper
    @APIResponse(
            responseCode = "400",
            description = "Validation of bundle failed",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))
    )
    public Response handleBundleRenderValidationException(BundleRenderValidationException exception) {
      return exception.getResponse();
    }
   [...]
}

public class BundleRenderValidationException extends WebApplicationException {
    public BundleRenderValidationException(String exceptionMessage) {
        super(
                Response
                        .status(Response.Status.BAD_REQUEST)
                        .entity(new ErrorResponse("Bundle Render Request failed validation", List.of(new String[]{exceptionMessage})))
                        .build()
        );
    }
}

Which would be the equivalent of the currently supported method:

@Path("/bundles")
@Produces({MediaType.APPLICATION_JSON})
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Bundles")
public class BundlesResource {
    [...]
    @POST
    @Path("/{bundleId}/actions/render")
    @APIResponses({
            @APIResponse(responseCode = "200", description = "Render bundle"),
    })
    public Uni<BundleRender> renderBundle(@RestPath long bundleId, BundleRenderRequest bundleRenderRequest) throws BundleRenderValidationException {
        return bundleRenderService.renderBundle(bundleId, bundleRenderRequest);
    }
   [...]
}

public class BundleRenderValidationException extends WebApplicationException {
    public BundleRenderValidationException(String exceptionMessage) {
        super(
                Response
                        .status(Response.Status.BAD_REQUEST)
                        .entity(new ErrorResponse("Bundle Render Request failed validation", List.of(new String[]{exceptionMessage})))
                        .build()
        );
    }
}

@Provider
public class BundleRenderValidationExceptionMapper implements ExceptionMapper<BundleRenderValidationException> {
    @Override
    @APIResponse(
            responseCode = "400",
            description = "Validation of bundle failed",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))
    )
    public Response toResponse(BundleRenderValidationException exception) {
        return exception.getResponse();
    }
}

which results in:

image

@MikeEdgar MikeEdgar added the enhancement New feature or request label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants