0

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.

Ise92
  • 127
  • 1
  • 1
  • 12
  • Can you give some examples of cases in which it _doesn't_ work? And yes, you do need to consider the exception mapper hierarchies. Closes exception wins, and there _are_ some default mappers built in. – Paul Samsotha Dec 29 '20 at 18:21
  • Hello Samsotha, I tried to get a NullPointerException, SqlException and JsonMapperException, when the deploy is ok and works all these 3 examples are caught correctly by my provider but when the provider is not correctly loaded none of these ejemples works... Is like sometimes my class exists and others not. I copied the full log when it works and when not to compare them with Meld and there isn't any differences between them, they 2 seems to load the class the same way. – Ise92 Dec 29 '20 at 20:18
  • I find out that my app uses a custom library that has another ExceptionMapper that maps Throwable like mine, I tried to debug it when my mapper is not working and bingo! Here it is! The reason why my mapper doesn't always work is because sometimes the compiler uses my class and other that one... Is there a way to make my mapper with more priority without changing any code of that other mapper? – Ise92 Dec 30 '20 at 09:46

0 Answers0