Search in sources :

Example 1 with ExceptionMessage

use of org.candlepin.common.exceptions.ExceptionMessage in project candlepin by candlepin.

the class JaxRsExceptionResponseBuilder method getResponse.

/**
 * Exceptions are thrown by the JAX-RS implementation when problems such as
 * incorrect types of parameters for JAX-RS resources are passed to a
 * method. Example would be string instead of a number passed as a
 * parameter. This method takes such an exception and builds pretty
 * undestandable HTTP resoponse for the client.
 *
 * @param exception
 *        JAX-RS implementation generated exception.
 * @return Response for the client
 */
public final Response getResponse(final Exception exception) {
    if (!canHandle(exception)) {
        throw new IllegalArgumentException("ResponseBuilder cannot handle this exception, you should call canHandle first", exception);
    }
    Map<String, String> map = VersionUtil.getVersionMap();
    ResponseBuilder bldr = Response.status(Status.BAD_REQUEST).type(cem.determineBestMediaType()).header(VersionUtil.VERSION_HEADER, map.get("version") + "-" + map.get("release"));
    Throwable cause = exception.getCause();
    if (cause instanceof CandlepinParameterParseException) {
        String msg = i18n.get().tr("Invalid format for query parameter {0}. Expected format: {1}", ((CandlepinParameterParseException) cause).getParamName(), ((CandlepinParameterParseException) cause).getExpectedFormat());
        bldr.entity(new ExceptionMessage(msg));
    } else {
        String msg = exception.getMessage();
        Matcher paramMatcher = PARAM_REGEX.matcher(msg);
        Matcher illegalValMatcher = ILLEGAL_VAL_REGEX.matcher(msg);
        paramMatcher.find();
        illegalValMatcher.find();
        String errorMessage = i18n.get().tr("{0} is not a valid value for {1}", illegalValMatcher.group(1), paramMatcher.group(1));
        bldr.entity(new ExceptionMessage(errorMessage));
    }
    return bldr.build();
}
Also used : CandlepinParameterParseException(org.candlepin.common.exceptions.CandlepinParameterParseException) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) Matcher(java.util.regex.Matcher) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 2 with ExceptionMessage

use of org.candlepin.common.exceptions.ExceptionMessage in project candlepin by candlepin.

the class CandlepinExceptionMapperTest method defaultBuilder.

@Test
public void defaultBuilder() {
    IOException ioe = new IOException("fake io exception");
    RuntimeException re = new RuntimeException("oops", ioe);
    ResponseBuilder bldr = cem.getDefaultBuilder(re, null, MediaType.APPLICATION_ATOM_XML_TYPE);
    Response r = bldr.build();
    ExceptionMessage em = (ExceptionMessage) r.getEntity();
    assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
    assertEquals(MediaType.APPLICATION_ATOM_XML_TYPE, r.getMetadata().get("Content-Type").get(0));
    assertTrue(em.getDisplayMessage().startsWith("Runtime Error " + re.getMessage()));
}
Also used : Response(javax.ws.rs.core.Response) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) IOException(java.io.IOException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Test(org.junit.Test)

Example 3 with ExceptionMessage

use of org.candlepin.common.exceptions.ExceptionMessage in project candlepin by candlepin.

the class CandlepinExceptionMapperTest method handleArrayIndexException.

@Test
public void handleArrayIndexException() {
    // Java 7 has a nice constructor where you can pass in false to not
    // fill in the stacktrace, but Java 6 does not have such a facility.
    // No amount of combination of creating Throwable without a stack
    // trace was working. We're resorting to using a mock version which
    // works great and is exactly the behavior we want.
    Throwable nostack = mock(Throwable.class);
    when(nostack.getMessage()).thenReturn("no stack trace");
    // simulate Java 7 on a Java 6 vm
    when(nostack.getStackTrace()).thenReturn(null);
    Response r = cem.getDefaultBuilder(nostack, null, MediaType.APPLICATION_JSON_TYPE).build();
    ExceptionMessage em = (ExceptionMessage) r.getEntity();
    assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
    assertEquals("Runtime Error no stack trace", em.getDisplayMessage());
    assertEquals(MediaType.APPLICATION_JSON_TYPE, r.getMetadata().get("Content-Type").get(0));
}
Also used : Response(javax.ws.rs.core.Response) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) Test(org.junit.Test)

Example 4 with ExceptionMessage

use of org.candlepin.common.exceptions.ExceptionMessage in project candlepin by candlepin.

the class CandlepinExceptionMapper method getDefaultBuilder.

protected ResponseBuilder getDefaultBuilder(Throwable exception, Status status, MediaType responseMediaType) {
    Throwable cause = exception;
    while (cause.getCause() != null) {
        cause = cause.getCause();
    }
    String message = "";
    StackTraceElement[] stes = cause.getStackTrace();
    // happen which breaks things.
    if (stes != null && stes.length > 0) {
        StackTraceElement ele = stes[0];
        int line = ele.getLineNumber();
        String method = ele.getMethodName();
        String clazz = ele.getClassName();
        message = i18n.get().tr("Runtime Error {0} at {1}.{2}:{3}", exception.getMessage(), clazz, method, line);
    } else {
        // just use the exception message
        message = i18n.get().tr("Runtime Error {0}", exception.getMessage());
    }
    if (!(exception instanceof CandlepinException) || ((CandlepinException) exception).isLogException()) {
        log.error(message, exception);
    }
    if (status == null) {
        status = Status.INTERNAL_SERVER_ERROR;
    }
    Map<String, String> map = VersionUtil.getVersionMap();
    return Response.status(status).entity(new ExceptionMessage(message)).type(responseMediaType).header(VersionUtil.VERSION_HEADER, map.get("version") + "-" + map.get("release"));
}
Also used : CandlepinException(org.candlepin.common.exceptions.CandlepinException) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage)

Example 5 with ExceptionMessage

use of org.candlepin.common.exceptions.ExceptionMessage in project candlepin by candlepin.

the class BadRequestExceptionMapper method toResponse.

@Override
public final Response toResponse(final BadRequestException exception) {
    if (badQueryParamHandler.canHandle(exception)) {
        return badQueryParamHandler.getResponse(exception);
    } else {
        // This is here to preserve old functionality.
        // It may be just as good to use getDefaultBuilder here
        Map<String, String> map = VersionUtil.getVersionMap();
        ResponseBuilder bldr = Response.status(Status.BAD_REQUEST).type(determineBestMediaType()).header(VersionUtil.VERSION_HEADER, map.get("version") + "-" + map.get("release"));
        // Use the message in the original exception if we have it
        String message = null;
        for (Throwable current = exception; current != null; current = current.getCause()) {
            message = current.getMessage();
        }
        // Set default message if we didn't have anything...
        if (message == null || message.isEmpty()) {
            message = I18n.marktr("Bad Request");
        }
        return bldr.entity(new ExceptionMessage(i18n.get().tr(message))).build();
    }
}
Also used : ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) JaxRsExceptionResponseBuilder(org.candlepin.common.util.JaxRsExceptionResponseBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

ExceptionMessage (org.candlepin.common.exceptions.ExceptionMessage)11 Response (javax.ws.rs.core.Response)6 Test (org.junit.Test)6 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 CandlepinException (org.candlepin.common.exceptions.CandlepinException)3 CandlepinParameterParseException (org.candlepin.common.exceptions.CandlepinParameterParseException)2 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 ConflictException (org.candlepin.common.exceptions.ConflictException)1 JaxRsExceptionResponseBuilder (org.candlepin.common.util.JaxRsExceptionResponseBuilder)1