Spark: Exception mappers not working with 2.8.0 when running on Tomcat

Created on 4 Oct 2018  路  10Comments  路  Source: perwendel/spark

The issue has to do with http://sparkjava.com/documentation#exception-mapping

I have confirmed that the same code that works fine with 2.7.2, stops working with 2.8.0, when running on Tomcat. Embedded Jetty is fine though.

Instead of invoking the custom ExceptionHandler, it invokes the default one ( spark.http.matching.GeneralError ).

Bug Major

Most helpful comment

Spark.exception method work with:
https://github.com/perwendel/spark/blob/master/src/main/java/spark/Service.java#L88

But SparkFilter for servlet use other instance:
https://github.com/perwendel/spark/blob/master/src/main/java/spark/ExceptionMapper.java#L39

Last project activity was in September, possible pull request for this problem already created.

Workaround:

  public synchronized <T extends Exception> void sevletException(Class<T> exceptionClass,
      ExceptionHandler<? super T> handler) {
    // wrap
    ExceptionHandlerImpl<T> wrapper = new ExceptionHandlerImpl<T>(exceptionClass) {
      @Override
      public void handle(T exception, Request request, Response response) {
        handler.handle(exception, request, response);
      }
    };

    ExceptionMapper.getServletInstance().map(exceptionClass, wrapper);
  }

and call sevletException with exception

    exception(Exception.class, new InternalServerErrorHandler());
    sevletException(Exception.class, new InternalServerErrorHandler());

All 10 comments

Spark.exception method work with:
https://github.com/perwendel/spark/blob/master/src/main/java/spark/Service.java#L88

But SparkFilter for servlet use other instance:
https://github.com/perwendel/spark/blob/master/src/main/java/spark/ExceptionMapper.java#L39

Last project activity was in September, possible pull request for this problem already created.

Workaround:

  public synchronized <T extends Exception> void sevletException(Class<T> exceptionClass,
      ExceptionHandler<? super T> handler) {
    // wrap
    ExceptionHandlerImpl<T> wrapper = new ExceptionHandlerImpl<T>(exceptionClass) {
      @Override
      public void handle(T exception, Request request, Response response) {
        handler.handle(exception, request, response);
      }
    };

    ExceptionMapper.getServletInstance().map(exceptionClass, wrapper);
  }

and call sevletException with exception

    exception(Exception.class, new InternalServerErrorHandler());
    sevletException(Exception.class, new InternalServerErrorHandler());

Is there any update on this issue? I would like to use Spark 2.8.0 because of the new awaitStop() method but I can't because of this issue. I wasn't able to apply a workaround so I moved back to 2.7.2 again.

Any chance this will be fixed?

The workaround doesn't work for me in version 2.9.1 anymore. Any chance for fixing it since it is such an advertised feature?

The workaround still works on 2.9.1, but it would be nice if this would be fixed soon.
At least it works after I ported the workaround to Kotlin.

Hello is there any progress regarding this problem?

Hello! Is there an update about this issue? Would be fixed in 2.9.2 version?

I would like to fix this issue @perwendel

Hello, I am really looking forward to solving this bug

Was this page helpful?
0 / 5 - 0 ratings