Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1532

UnhandledException throw from SynchronousDispatcher writeException not goto ExceptionMapper

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Critical Critical
    • 3.0.23.Final, 3.1.3.Final
    • 3.0.19.Final
    • jaxrs
    • None
    • Hide

      1. integrate spring with resteasy
      2. define ExceptionMapper

      @Provider
      public class CustomExceptionMapper implements ExceptionMapper<Throwable> {
          private Logger logger = LogManager.getLogger(CustomExceptionMapper.class);
      
          @Override
          public Response toResponse(Throwable e) {
              logger.error("encounter exception!!!", e);
      
              Map<String, Object> result = new HashMap<>();
              result.put("err_msg", "UN_KNOWN_ERR");
              result.put("err_detail", "please contact admin for help");
      
              return Response.status(Response.Status.OK).entity(result)
                      .type(MediaType.APPLICATION_JSON_TYPE).build();
          }
      }
      

      3. define an empty entity

      public class MyEntity {
      }
      

      4. return entity in resource

      @Component
      @Path("/test")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public class TestResource {
          @GET
          public Response test() {
              MyEntity entity = new MyEntity();
              return Response.status(Response.Status.OK).entity(entity).build();
          }
      }
      

      5. request the resource then the exception mentioned in description section print into console but my ExceptionMapper can not get any exception information.

      Show
      1. integrate spring with resteasy 2. define ExceptionMapper @Provider public class CustomExceptionMapper implements ExceptionMapper<Throwable> { private Logger logger = LogManager.getLogger(CustomExceptionMapper.class); @Override public Response toResponse(Throwable e) { logger.error( "encounter exception!!!" , e); Map< String , Object > result = new HashMap<>(); result.put( "err_msg" , "UN_KNOWN_ERR" ); result.put( "err_detail" , "please contact admin for help" ); return Response.status(Response.Status.OK).entity(result) .type(MediaType.APPLICATION_JSON_TYPE).build(); } } 3. define an empty entity public class MyEntity { } 4. return entity in resource @Component @Path( "/test" ) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class TestResource { @GET public Response test() { MyEntity entity = new MyEntity(); return Response.status(Response.Status.OK).entity(entity).build(); } } 5. request the resource then the exception mentioned in description section print into console but my ExceptionMapper can not get any exception information.
    • Workaround Exists
    • Hide

      Manually serialize the entity to json string before set the result into response entity like below but it just seems not pretty coded:

      @GET
      public Response test() throws JsonProcessingException {
          MyEntity entity = new MyEntity();
          ObjectMapper objectMapper = new ObjectMapper();
          String jsonStr = objectMapper.writeValueAsString(entity);
          return Response.status(Response.Status.OK).entity(jsonStr).build();
      }
      
      Show
      Manually serialize the entity to json string before set the result into response entity like below but it just seems not pretty coded: @GET public Response test() throws JsonProcessingException { MyEntity entity = new MyEntity(); ObjectMapper objectMapper = new ObjectMapper(); String jsonStr = objectMapper.writeValueAsString(entity); return Response.status(Response.Status.OK).entity(jsonStr).build(); }

      I have integrated spring with resteasy and registered HttpServletDispatcher as servlet.
      I customized my own ExceptionMapper to deal with all Throwable, and by the way I have tested that it works in most cases.
      But when there is an exception caught in SynchronousDispatcher(I just track step by step), it does not go to my ExceptionMapper and just print stack info into console. The exception is:

      ERROR [io.undertow.request] (default task-8) UT005023: Exception handling request to /test: org.jboss.resteasy.spi.UnhandledException: RESTEASY003770: Response is committed, can't handle exception
      	at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:174)
      	at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:478)
      	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:422)
      	at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:209)
      	at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
      	at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
      	at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      	at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
      	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
      	at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
      	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
      	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
      	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
      	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
      	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
      	at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
      	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
      	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
      	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
      	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
      	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
      	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
      	at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
      	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
      	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      	at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
      	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
      	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
      	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
      	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
      	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
      	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
      	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
      	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
      	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
      	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
      	at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
      	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
      	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
      	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
      	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
      	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:745)
      Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class cn.xz.entity.MyEntity and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
      	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:269)
      	at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:68)
      	at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
      	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
      	at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1428)
      	at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:930)
      	at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.writeTo(ResteasyJackson2Provider.java:207)
      	at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:131)
      	at org.jboss.resteasy.core.interception.ServerWriterInterceptorContext.writeTo(ServerWriterInterceptorContext.java:60)
      	at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:120)
      	at org.jboss.resteasy.security.doseta.DigitalSigningInterceptor.aroundWriteTo(DigitalSigningInterceptor.java:145)
      	at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124)
      	at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100)
      	at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:124)
      	at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:98)
      	at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:473)
      	... 48 more
      

            rhn-support-asoldano Alessio Soldano
            xuanzhui zhui xuan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: