1   package com.atlassian.plugins.rest.common.error.jersey;
2   
3   import com.atlassian.plugins.rest.common.Status;
4   
5   import javax.ws.rs.WebApplicationException;
6   import javax.ws.rs.core.Response;
7   import javax.ws.rs.ext.ExceptionMapper;
8   import javax.ws.rs.ext.Provider;
9   
10  /**
11   * A generic exception mapper that will map any {@link Throwable throwable}.  Handles the special case of
12   * {@link WebApplicationException}, which provides its own response.
13   *
14   * @since 1.0
15   */
16  @Provider
17  public class ThrowableExceptionMapper implements ExceptionMapper<Throwable>
18  {
19      public Response toResponse(Throwable t)
20      {
21          if (t instanceof WebApplicationException)
22          {
23              return ((WebApplicationException)t).getResponse();
24          }
25          else
26          {
27              return Status.error().message(t.getMessage()).response();
28          }
29  
30      }
31  }