2

I have an ExceptionMapper as a @Provider for handling all of my exceptions.

So it is obvious that my class implements ExceptionMapper<Throwable> and we as know, all exceptions are extended Throwable somehow.

Now I used jax-rs @NotNull to checking my resources input values to be not null and by some search, I realized it will throw ConstraintViolationException when the annotated field is null.

So I tried to handle it and add some details to response (add some custom json model) in my ExceptionMapper like this:

@Provider
public class AllExceptionMapper implements ExceptionMapper<Throwable> {

    private final Logger logger = LogManager.getLogger(AllExceptionMapper.class);

    @Override
    public Response toResponse(Throwable ex) {
        Response response;
        CustomError error;
        // handle some errors

        else if (ex instanceof ConstraintViolationException) {
             error = new CustomError(
                 324, // predefined error type in our documents
                 "some details"
             );
             response = Response.status(Response.Status.BAD_REQUEST).entity(error).build();
        }

        // handle some other errors

        return response;
    }

}

The problem is this didn't work but if I would create another exception mapper provider which implements ExceptionMapper<ConstraintViolationException> and handle it there, it works without any problem.

As I said before (and also checked) all exceptions are extended from Throwable class somehow so what am I missing and why it is not working?

......

By didn't work, I mean it ignores my mapper (the one which implements from ExceptionMapper<Throwable>) and had the normal behavior which is returning the status code 400 with no response payload like there is no mapper at all

svarog
  • 9,477
  • 4
  • 61
  • 77
Seyed Ali Roshan
  • 1,476
  • 1
  • 18
  • 37
  • What do you mean by **this didn't work**? What is the current behaviour? – SBylemans Oct 30 '18 at 11:22
  • @SBylemans it ignores my mapper (the one which implements `Throwable`) and acts normal like there is no mapper at all – Seyed Ali Roshan Oct 30 '18 at 11:25
  • Not sure what could cause it, but maybe there's already a built-in mapper that that implements the `ExceptionMapper` with a generic class that is lower in the inheritance tree? – SBylemans Oct 30 '18 at 11:30
  • @SBylemans is there any lower in the inheritance tree than `Throwable` for exceptions? – Seyed Ali Roshan Oct 30 '18 at 11:31
  • @SBylemans I don't know is it helpful or not but I am using `spring-boot-starter-jersey` – Seyed Ali Roshan Oct 30 '18 at 11:33
  • I mean higher... Is there a reason why you don't want it in another class implementing `ExceptionMapper`? That seems to me the better solution, which gives you more code separation and such... – SBylemans Oct 30 '18 at 11:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182787/discussion-between-seyed-ali-roshan-and-sbylemans). – Seyed Ali Roshan Oct 30 '18 at 11:36

2 Answers2

2

The way the ExceptionMapper is supposed to work, is that you can create a generic ExceptionMapper<Throwable>, which handles all errors. And then you can create more specific ExceptionMappers which handle the more specific errors in another way. All of this in separate classes.

The fact that doing it in a separate class works for you, lets me believe that there's a more specific ExceptionMapper somewhere, which handles the exception before you can.

The way ExceptionMappers are intended to be used is actually very clean, and also keeps your code clean. Wanting to keep the code in one central place, will result in a giant if...

SBylemans
  • 1,764
  • 13
  • 28
0

Don't using @Privider and should be using 'ResourceConfig.register(MyExceptionMapper);'

 public class ErrorHandler implements ExceptionMapper<Throwable> {

    private final static Logger LOGGER = LoggerFactory.getLogger(ErrorHandler.class);

    @Override
    public Response toResponse(Throwable e) {

        LOGGER.error("Handle a unknown error, " + e.getClass() + ": " + e.getMessage(), e);
        e.printStackTrace();

        Response.ResponseBuilder responseBuilder = Response.status(status);
        responseBuilder.entity(exception);

        return responseBuilder.build();
    }
}


class Config extends ResourceConfig {

    private final static Logger LOGGER = LoggerFactory.getLogger(Main.class);

    public Config() {
        super();
        super.register(ErrorHandler.class);
    }
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Jim
  • 31
  • 1