View Javadoc

1   package com.atlassian.plugins.rest.common.error.jersey;
2   
3   import javax.ws.rs.WebApplicationException;
4   import javax.ws.rs.core.Context;
5   import javax.ws.rs.core.Request;
6   import javax.ws.rs.core.Response;
7   import javax.ws.rs.ext.ExceptionMapper;
8   import javax.ws.rs.ext.Provider;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  /**
14   * A generic exception mapper that will map any {@link Throwable throwable}.  Handles the special case of
15   * {@link WebApplicationException}, which provides its own response.
16   *
17   * @since 1.0
18   */
19  @Provider
20  public class ThrowableExceptionMapper implements ExceptionMapper<Throwable>
21  {
22      private static final Logger log = LoggerFactory.getLogger(ThrowableExceptionMapper.class);
23  
24      @Context
25      Request request;
26  
27      public Response toResponse(Throwable t)
28      {
29          log.error("Uncaught exception thrown by REST service", t);
30  
31          if (t instanceof WebApplicationException)
32          {
33              return ((WebApplicationException)t).getResponse();
34          }
35          else
36          {
37              return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
38                      .entity(new UncaughtExceptionEntity(t))
39                      .type(UncaughtExceptionEntity.variantFor(request))
40                      .build();
41          }
42      }
43  }