예제 #1
0
  @Test
  public void exceptionMeteredMethodsAreExceptionMetered() {
    final Meter meter =
        Metrics.newMeter(
            InstrumentedResource.class, "exceptionMeteredExceptions", "blah", TimeUnit.SECONDS);

    assertThat(resource().path("exception-metered").get(String.class), is("fuh"));

    assertThat(meter.count(), is(0L));

    try {
      resource().path("exception-metered").queryParam("splode", "true").get(String.class);
      fail("should have thrown a MappableContainerException, but didn't");
    } catch (MappableContainerException e) {
      assertThat(e.getCause(), is(instanceOf(IOException.class)));
    }

    assertThat(meter.count(), is(1L));
  }
  /**
   * Map the cause of a mappable container exception to a response.
   *
   * <p>If the cause cannot be mapped and then that cause is re-thrown if a runtime exception
   * otherwise the mappable container exception is re-thrown.
   *
   * @param e the mappable container exception whose cause will be mapped to a response.
   */
  public void mapMappableContainerException(MappableContainerException e) {
    Throwable cause = e.getCause();

    if (cause instanceof WebApplicationException) {
      mapWebApplicationException((WebApplicationException) cause);
    } else if (!mapException(cause)) {
      if (cause instanceof RuntimeException) {
        LOGGER.log(
            Level.SEVERE,
            "The RuntimeException could not be mapped to a response, "
                + "re-throwing to the HTTP container",
            cause);
        throw (RuntimeException) cause;
      } else {
        LOGGER.log(
            Level.SEVERE,
            "The exception contained within "
                + "MappableContainerException could not be mapped to a response, "
                + "re-throwing to the HTTP container",
            cause);
        throw e;
      }
    }
  }