public boolean executeExceptionMapperForClass(
     HttpRequest request, HttpResponse response, Throwable exception, Class clazz) {
   ExceptionMapper mapper = providerFactory.getExceptionMapper(clazz);
   if (mapper == null) return false;
   writeFailure(request, response, mapper.toResponse(exception));
   return true;
 }
  protected void handleWebApplicationException(
      HttpRequest request, HttpResponse response, WebApplicationException wae) {
    if (!(wae instanceof NoLogWebApplicationException)) logger.error("failed to execute", wae);
    if (response.isCommitted())
      throw new UnhandledException("Request was committed couldn't handle exception", wae);

    writeFailure(request, response, wae.getResponse());
  }
  /**
   * Execute an ExceptionMapper if one exists for the given exception. Recurse to base class if not
   * found
   *
   * @param response
   * @param exception
   * @return true if an ExceptionMapper was found and executed
   */
  public boolean executeExceptionMapper(
      HttpRequest request, HttpResponse response, Throwable exception) {
    ExceptionMapper mapper = null;

    Class causeClass = exception.getClass();
    while (mapper == null) {
      if (causeClass == null) break;
      mapper = providerFactory.getExceptionMapper(causeClass);
      if (mapper == null) causeClass = causeClass.getSuperclass();
    }
    if (mapper != null) {
      Response jaxrsResponse = mapper.toResponse(exception);
      if (jaxrsResponse == null) {
        jaxrsResponse = Response.status(204).build();
      }
      writeFailure(request, response, jaxrsResponse);
      return true;
    }
    return false;
  }
  protected void handleFailure(HttpRequest request, HttpResponse response, Failure failure) {
    if (failure.isLoggable())
      logger.error(
          "Failed executing " + request.getHttpMethod() + " " + request.getUri().getPath(),
          failure);
    else
      logger.debug(
          "Failed executing " + request.getHttpMethod() + " " + request.getUri().getPath(),
          failure);

    if (failure.getResponse() != null) {
      writeFailure(request, response, failure.getResponse());
    } else {
      try {
        if (failure.getMessage() != null) {
          response.sendError(failure.getErrorCode(), failure.getMessage());
        } else {
          response.sendError(failure.getErrorCode());
        }
      } catch (IOException e1) {
        throw new UnhandledException(e1);
      }
    }
  }