JAX-RS interceptor mapping exceptions on void methods

Viewed 271

I am trying to use an interceptor to introspect and change exceptions that occur on the backend. The interceptor looks something like this:

public class ApplicationErrorInterceptor {
    private static final Logger LOGGER = Logger.getLogger(ApplicationErrorInterceptor.class);

    @AroundInvoke
    public Object handleException(InvocationContext context) {
        try {
            return context.proceed();
        } catch (Exception ex) {
            LOGGER.errorf(ex, "An unhandled exception occured!");
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity("some custom 500 error text")
                    .build();
        }
    }
}

And the service using this interceptor looks something like this:

@Path("/somepath")
@Interceptors({ApplicationErrorInterceptor.class})
public interface SomeRestService {
    @POST
    @Path("getResponse")
    Response getResponse();

    @POST
    @Path("getVoid")
    void getVoid();
}

Assume the implementation for both of these methods throw an exception. I expect the exception to be mapped to a 500 server error with the supplied custom message in both cases. Unfortunately, the method returning void will get mapped to a 204 No Content response. If I remove the interceptor altogether, the default 500 server error occurs, which is at least the correct status code, but I lost the error customization

0 Answers
Related