title: Jersey error response wrapped by websphere "com.ibm.ws.webcontainer.webapp.WebAppErrorReport: Internal Server Error"
body: <p>We have a Jersey service where error messages does not return to the calling client because Websphere is wrapping the error. The service is working as expected for Status.OK response. Our problem is when we want to handle exceptions. We disabled wink (<a href="https://www.ibm.com/support/knowledgecenter/SSAW57_8.0.0/com.ibm.websphere.nd.doc/info/ae/ae/twbs_jaxrs_disable_runtime.html" rel="nofollow">instructions here</a>) in our WebSphere server and bundled Jersey jars in our WAR file. </p>
  
  <p>Here's an example Websphere console logs:</p>
  
  <pre><code>0000010b webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[our-service]: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: Internal Server Error
  at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendError(WebAppDispatcherContext.java:632)
  at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendError(WebAppDispatcherContext.java:662)
  at com.ibm.ws.webcontainer.srt.SRTServletResponse.sendError(SRTServletResponse.java:1301)
  at org.glassfish.jersey.servlet.internal.ResponseWriter.commit(ResponseWriter.java:199)
  at org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:413)
  at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:691)
  at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:377)
  at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:420)
  at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
  at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
  at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
  at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
  at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030)
  at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
  </code></pre>
  
  <p>We have the following exception mapper:</p>
  
  <pre><code>package org.sample.exception.mapper;
  
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.core.Response.Status;
  import javax.ws.rs.ext.ExceptionMapper;
  import javax.ws.rs.ext.Provider;
  
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  
  @Provider
  public class SampleExceptionMapper
    implements ExceptionMapper&lt;Exception&gt; {
  
    private static final Logger logger = LogManager.getLogger( SampleExceptionMapper.class.getName() );
  
    @Override
    public Response toResponse( Exception ex ) {
      String message = ex.getClass().getCanonicalName() + " - " + ex.getMessage();
      return Response.status( Status.SERVICE_UNAVAILABLE.getStatusCode() ).entity( message ).type( MediaType.TEXT_PLAIN ).build();
    }
  }
  </code></pre>
  
  <p>and in our Service implementation, we throw the following exception when we have problems sending an email:</p>
  
  <pre><code>package org.sample.common.exception;
  
  public class EmailSendingException
    extends BaseServiceException {
  
    public EmailSendingException() {
      super( ServiceException.EMAIL_SENDING_FAILED );
    }
  }
  
  public enum ServiceException {
  
    EMAIL_SENDING_FAILED( "Email sending failed" );
  
    private String description;
  
    private ServiceException( String description ) {
      this.description = description;
    }
  
    public String getDescription() {
      return description;
    }
  }
  </code></pre>
  
  <p>Sample code that calls the service:</p>
  
  <pre><code>  @Override
    public boolean saveData( SampleData sampleData )
      throws EmailSendingException, WebServiceException {
      WebTarget client = RestClientBuilder.getRestClient( this.SAMPLE_URL );
      Response response = client.request().post( Entity.entity( sampleData, MediaType.APPLICATION_JSON ) );
      boolean isSaveSuccess = false;
      if ( Status.OK.getStatusCode() == response.getStatus() ) {
        logger.info( "saving data..." );
        isSaveSuccess = true;
      }
      else {
        String result = response.readEntity( String.class );
        logger.info( "result: " + result );
        if ( result.contains( "Email sending failed" ) ) {
          throw new EmailSendingException(); // EXPECTED TO ENTER HERE
        }
        else {
          throw new WebServiceException(); // BUT ALWAYS GOES HERE
        }
      }
      return isSaveSuccess;
    }
  </code></pre>
  
  <p>What do we need to do to let the error message reach the calling client class?</p>