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
Throwable) and acts normal like there is no mapper at all - Seyed Ali RoshanExceptionMapperwith a generic class that is lower in the inheritance tree? - SBylemansThrowablefor exceptions? - Seyed Ali Roshanspring-boot-starter-jersey- Seyed Ali Roshan