/**
  * Map a web application exception to a response.
  *
  * @param e the web application exception.
  */
 public void mapWebApplicationException(WebApplicationException e) {
   if (e.getResponse().getEntity() != null) {
     wa.getResponseListener().onError(Thread.currentThread().getId(), e);
     onException(e, e.getResponse(), false);
   } else {
     if (!mapException(e)) {
       onException(e, e.getResponse(), false);
     }
   }
 }
  /**
   * Map an exception to a response.
   *
   * @param e the exception.
   * @return true if the exception was mapped, otherwise false.
   */
  public boolean mapException(Throwable e) {
    ExceptionMapper em = wa.getExceptionMapperContext().find(e.getClass());
    if (em == null) {
      wa.getResponseListener().onError(Thread.currentThread().getId(), e);

      return false;
    }

    wa.getResponseListener().onMappedException(Thread.currentThread().getId(), e, em);

    if (request.isTracingEnabled()) {
      request.trace(
          String.format(
              "matched exception mapper: %s -> %s",
              ReflectionHelper.objectToString(e), ReflectionHelper.objectToString(em)));
    }

    try {
      Response r = em.toResponse(e);
      if (r == null) r = Response.noContent().build();
      onException(e, r, true);
    } catch (MappableContainerException ex) {
      // If the exception mapper throws a MappableContainerException then
      // rethrow it to the HTTP container
      throw ex;
    } catch (RuntimeException ex) {
      LOGGER.severe(
          "Exception mapper "
              + em
              + " for Throwable "
              + e
              + " threw a RuntimeException when "
              + "attempting to obtain the response");
      Response r = Response.serverError().build();
      onException(ex, r, false);
    }
    return true;
  }