title: Error handling in REST API with JAX-RS
body: <p><strong>The task:</strong> Instead of receiving general <code>HTTP 500 Internal Server Error</code> in my stacktrace and the same horrible stacktrace on the client side I want to see my customized message with another statuscode (<code>403</code> for example), that it will be much clearer for the developer, what has happend. And add some message to User about the Exception.</p>
  
  <p>Here are couple of changed classes from my application:</p>
  
  <p><strong><em>SERVER PART:</em></strong></p>
  
  <p><em><code>AppException.class</code></em> - all my Server Response exceptions (before giving back to client) I want to transform into this exception. Kinda standard entity class</p>
  
  <pre><code>public class AppException extends WebApplicationException {
  
  Integer status;
  
  /** application specific error code */
  int code;
  
  /** link documenting the exception */
  String link;
  
  /** detailed error description for developers */
  String developerMessage;
  
  public AppException(int status, int code, String message, String developerMessage, String link) {
      super(message);
      this.status = status;
      this.code = code;
      this.developerMessage = developerMessage;
      this.link = link;
  }
  
  public int getStatus() {
      return status;
  }
  
  public void setStatus(int status) {
      this.status = status;
  }
  
  public int getCode() {
      return code;
  }
  
  public void setCode(int code) {
      this.code = code;
  }
  
  public String getDeveloperMessage() {
      return developerMessage;
  }
  
  public void setDeveloperMessage(String developerMessage) {
      this.developerMessage = developerMessage;
  }
  
  public String getLink() {
      return link;
  }
  
  public void setLink(String link) {
      this.link = link;
  }
  
  public AppException() {
  }
  
  public AppException(String message) {
      super("Something went wrong on the server");
  }
  }
  </code></pre>
  
  <p><em><code>ÀppExceptionMapper.class</code></em> - mapps my AppException to the JAX-RS Runtime, instead standard exception, client receives AppException. </p>
  
  <pre><code>    @Provider
  public class AppExceptionMapper implements ExceptionMapper&lt;AppException&gt; {
  
      @Override
      public Response toResponse(AppException exception) {
          return Response.status(403)
                  .entity("toResponse entity").type("text/plain").build();
      }
  
  
  }
  </code></pre>
  
  <p><em><code>ApplicationService.class</code></em>- my Service class that throws AppException</p>
  
  <pre><code> @Path("/applications")
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public interface ApplicationService {
  
  
      @DELETE
      @Path("/deleteById")
      void deleteById(@NotNull Long id) throws AppException;
  }
  </code></pre>
  
  <p><strong><em>CLIENT PART:</em></strong></p>
  
  <p><em><code>ErrorHandlingFilter.class</code></em>- my Response catcher of the AppException. Here I want to transform each Response exception to another exception depending on the status. </p>
  
  <pre><code>@Provider
  public class ErrorHandlingFilter implements ClientResponseFilter {
  
      private static ObjectMapper _MAPPER = new ObjectMapper();
  
      @Override
      public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
          if (responseContext.getStatus() != Response.Status.OK.getStatusCode()) {
              if(responseContext.hasEntity()) {
                  Error error = _MAPPER.readValue(responseContext.getEntityStream(), Error.class);
                  String message = error.getMessage();
  
                  Response.Status status = Response.Status.fromStatusCode(responseContext.getStatus());
                  AppException clientException;
  
                  switch (status) {
  
                  case INTERNAL_SERVER_ERROR:
                      clientException = new PermissionException(message);
                      break;
  
  
                  case NOT_FOUND:
                      clientException = new MyNotFoundException(message);
                      break;
  
                  default:
                      clientException =  new WhatEverException(message);
                  }
                      throw clientException;
          }
      }
      }
  }
  </code></pre>
  
  <p><em><code>PermissionException.class</code></em> - exception in what I want to transform AppException, if it came with 500 status code.</p>
  
  <pre><code>public class PermissionException extends AppException{
  
          public PermissionException(String message) {
      super("403 - Forbidden. You dont have enough rights to delete this Application");
  
  }
  
  Integer status;
  
  /** application specific error code */
  int code;
  
  /** link documenting the exception */
  String link;
  
  /** detailed error description for developers */
  String developerMessage;
  
  public PermissionException(int status, int code, String message, String developerMessage, String link) {
      super(message);
      this.status = status;
      this.code = code;
      this.developerMessage = developerMessage;
      this.link = link;
  }
  
  public int getStatus() {
      return status;
  }
  
  public void setStatus(int status) {
      this.status = status;
  }
  
  public int getCode() {
      return code;
  }
  
  public void setCode(int code) {
      this.code = code;
  }
  
  public String getDeveloperMessage() {
      return developerMessage;
  }
  
  public void setDeveloperMessage(String developerMessage) {
      this.developerMessage = developerMessage;
  }
  
  public String getLink() {
      return link;
  }
  
  public void setLink(String link) {
      this.link = link;
  }
  
  public PermissionException() {}
  
  
  }
  </code></pre>
  
  <p><em><code>ApplicationPresenter.class</code></em>- piece of UI logic, where I want something to do with PermissionException thrown by the ErrorHandlingFilter.</p>
  
  <pre><code>@SpringPresenter
  public class ApplicationPresenter implements ApplicationView.Observer {
  
  @Resource
      private ApplicationService applicationService;
  
      @Resource
      private UiEnvironment uiEnvironment;
  
  @Override
      public void deleteSelectedApplication(BeanItemGrid&lt;Application&gt; applicationGrid) {
  
          try {
  applicationService.deleteById(applicationGrid.getSelectedItem().getId());
                      } catch (PermissionException e) {
                          e.printStackTrace();
                          e.getMessage();
                      } catch (AppException e2) {
                      }
  }
  }
  </code></pre>
  
  <p>How can I resolve my problem? I am still receiving standard <code>500 InternalErrorException.</code> </p>
  
  <p><strong>UPDATED ALMOST THE WHOLE QUESTION ONE MORE TIME!</strong></p>