Search in sources :

Example 6 with ExceptionMessage

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

the class ValidationExceptionMapper method toResponse.

@Override
public Response toResponse(ValidationException exception) {
    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"));
    StringBuffer message = new StringBuffer();
    if (ConstraintViolationException.class.isAssignableFrom(exception.getClass())) {
        for (ConstraintViolation cv : ((ConstraintViolationException) exception).getConstraintViolations()) {
            message.append(cv.getPropertyPath().toString());
            message.append(": ");
            message.append(cv.getMessage());
        }
        bldr.entity(new ExceptionMessage(message.toString()));
    } else {
        getDefaultBuilder(exception, Response.Status.BAD_REQUEST, determineBestMediaType());
    }
    return bldr.build();
}
Also used : ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 7 with ExceptionMessage

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

the class RuntimeExceptionMapperTest method candlepinException.

@Test
public void candlepinException() {
    CandlepinException ce = mock(CandlepinException.class);
    when(ce.httpReturnCode()).thenReturn(Status.CONFLICT);
    ExceptionMessage em = new ExceptionMessage().setDisplayMessage("you screwed up");
    when(ce.message()).thenReturn(em);
    when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json");
    assertEquals("you screwed up", em.toString());
    Response r = rem.toResponse(ce);
    assertNotNull(r);
    assertEquals(Status.CONFLICT.getStatusCode(), r.getStatus());
    verifyMessage(r, "you screwed up");
}
Also used : Response(javax.ws.rs.core.Response) CandlepinException(org.candlepin.common.exceptions.CandlepinException) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) Test(org.junit.Test)

Example 8 with ExceptionMessage

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

the class RuntimeExceptionMapperTest method candlepinExceptionWithChildNotCandleping.

@Test
public void candlepinExceptionWithChildNotCandleping() {
    CandlepinException ce = mock(CandlepinException.class);
    when(ce.httpReturnCode()).thenReturn(Status.CONFLICT);
    when(ce.message()).thenReturn(new ExceptionMessage().setDisplayMessage("you screwed up"));
    when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json");
    when(ce.getCause()).thenReturn(new ConflictException("you screwed up"));
    Response r = rem.toResponse(ce);
    assertNotNull(r);
    assertEquals(Status.CONFLICT.getStatusCode(), r.getStatus());
    verifyMessage(r, "you screwed up");
}
Also used : Response(javax.ws.rs.core.Response) CandlepinException(org.candlepin.common.exceptions.CandlepinException) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) ConflictException(org.candlepin.common.exceptions.ConflictException) Test(org.junit.Test)

Example 9 with ExceptionMessage

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

the class TestExceptionMapperBase method verifyMessage.

protected void verifyMessage(Response r, String expectedmsg) {
    ExceptionMessage em = (ExceptionMessage) r.getEntity();
    assertNotNull(em);
    assertThat(em.getDisplayMessage(), matchesRegex(expectedmsg));
}
Also used : ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage)

Example 10 with ExceptionMessage

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

the class JaxRsExceptionResponseBuilderTest method candlepinParserError.

@Test
public void candlepinParserError() {
    CandlepinParameterParseException parseEx = new CandlepinParameterParseException("paramName", "thisFormat");
    RuntimeException ex = new RuntimeException(parseEx);
    Response resp = exceptionBuilder.getResponse(ex);
    ExceptionMessage e = (ExceptionMessage) resp.getEntity();
    assertTrue(exceptionBuilder.canHandle(ex));
    assertTrue(e.getDisplayMessage().contains("paramName"));
    assertTrue(e.getDisplayMessage().contains("thisFormat"));
}
Also used : CandlepinParameterParseException(org.candlepin.common.exceptions.CandlepinParameterParseException) Response(javax.ws.rs.core.Response) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) Test(org.junit.Test)

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