I'm trying to make a global ExceptionMapper for my app, this ExceptionMapper will show the excepction in console, then will show it again in a beautify way and then save some info of it in the database to make it easier in the future to identify these incidents.
My app uses com.sun.jersey.spi.spring.container.servlet.SpringServlet
and I find out that I can intercept exceptions using a Provider with ExceptionMapper, so I made this one:
package es.companyName.appName.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Named
@Priority(1)
@Provider
public class UncaughtExceptionHandler extends Throwable implements ExceptionMapper<Throwable> {
private static final long serialVersionUID = 1L;
private Logger logger = LoggerFactory.getLogger(this.getClass());
public UncaughtExceptionHandler () {
logger.info("Provider UncaughtExceptionHandler creado correctamente");
}
@Override
public Response toResponse(Throwable ex) {
//some stuff
...
}
}
And I declared it in my web.xml this way:
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>es.companyName.appName.rs, es.companyName.appName.config</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
With this my ExceptionMapper is working fine but for some reason it doesn't always work, when I compile and deploy my app sometimes works and sometimes not, without changing any code.
I tried to debug my ExceptionMapper when it doesn't work and it is not entering in there, is like my ExceptionMapper is not loading properly... In console it appears that is loading correctly:
appName 2020-12-29 14:45:10.638 --- appName.config.UncaughtExceptionHandler : Provider UncaughtExceptionHandler creado correctamente
...
INFO: Scanning for root resource and provider classes in the packages:
es.companyName.appName.rs
es.companyName.appName.config
Dec 29, 2020 4:43:28 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
......
Dec 29, 2020 4:43:28 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
class es.companyName.appName.config.UncaughtExceptionHandler
But it is not because I can't stop there with debug, is like the deploy is ignoring my mapper... Is there anything that I left on? It should be some configuration thing that I'm missing but the strange thing is that sometimes it works correctly and I can even stop with debugger in there.
I read in some related articles that say since jersey is trying to find the best match for the given exceptions within it's list of mappers (which includes some jersey default mappers), you have to map your mapper to the exact exception you are trying to catch, since the order of the mapper is arbitrary but in my case I don't want to map some specific exception but all the exceptions in my app.
Since I don't have any other mapper in my app the ones that are doing this should be the jersey default mappers, is there a way to turn them off? Or am I missing something?
Thank you very much for your answers.