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

Custom ExceptionMapper mapper is not catching swagger validation #3083

Closed
ktalebian opened this issue Dec 26, 2019 · 2 comments
Closed

Custom ExceptionMapper mapper is not catching swagger validation #3083

ktalebian opened this issue Dec 26, 2019 · 2 comments
Labels
stale Stale issue or pull request which will be closed soon

Comments

@ktalebian
Copy link

I have a custom exception mapper:

@Provider
public class AppExceptionMapper implements ExceptionMapper<Exception> {
    @Context
    private HttpServletRequest httpRequest;
  
    @Override
    public Response toResponse(final Exception e) {
        if (e instanceof ValidationException) {
           // return a custom Response here
        } 
        // other mappers
    }
}

And have it registered as environment.jersey().register(new AppExceptionMapper());

This would work when in my code I do throw new ValidationException(...). However, I have swagger annotations for validating the request:

@POST
public void doSomething(@Valid @NotNull final SomeObject object);

When this validation fails, my custom exception mapper does not catch it, and the generic exception is returned. I put a breakpoint inside ValidationException, and swagger does throw this exception, but for some reason, my exception mapper is not catching it.

How come?

@reneploetz
Copy link

reneploetz commented Dec 27, 2019

(These are likely not swagger annotations, but javax.validation / hibernate-validator annotations.)

As you also commented in #2380 and eclipse-ee4j/jersey#3425 I would assume you tried to register this exact class in Jersey.

Did you try the approach stated in the upstream ticket you commented on?

environment.jersey().register(new AbstractBinder() {
    @Override
    protected void configure() {
        bind(CustomExceptionMapper.class).to(ExceptionMapper.class).in(Singleton.class);
    }
});

Note that the referenced CustomExceptionMapperclass must implement javax.ws.rs.ext.ExceptionMapper<ValidationException> and therefore use javax.validation.ValidationException as parameter to toResponse to override the default jersey implementation (and Dropwizards override itself unless you already disabled that with registerDefaultExceptionMappers=false). This means you cannot use a generic exception mapper for the whole application.

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale Stale issue or pull request which will be closed soon label Mar 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Stale issue or pull request which will be closed soon
Projects
None yet
Development

No branches or pull requests

2 participants