Search in sources :

Example 1 with ApiError

use of org.graylog2.plugin.rest.ApiError in project graylog2-server by Graylog2.

the class JacksonPropertyExceptionMapper method toResponse.

@Override
public Response toResponse(PropertyBindingException e) {
    final Collection<Object> knownPropertyIds = firstNonNull(e.getKnownPropertyIds(), Collections.emptyList());
    final ApiError apiError = new ApiError("Unable to map property " + e.getPropertyName() + ".\nKnown properties include: " + Joiner.on(", ").join(knownPropertyIds));
    return status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(apiError).build();
}
Also used : ApiError(org.graylog2.plugin.rest.ApiError)

Example 2 with ApiError

use of org.graylog2.plugin.rest.ApiError in project graylog2-server by Graylog2.

the class JsonProcessingExceptionMapperTest method testToResponse.

@Test
public void testToResponse() throws Exception {
    final ExceptionMapper<JsonProcessingException> mapper = new JsonProcessingExceptionMapper();
    final JsonParser jsonParser = new JsonFactory().createParser("");
    final JsonMappingException exception = new JsonMappingException(jsonParser, "Boom!", new RuntimeException("rootCause"));
    final Response response = mapper.toResponse(exception);
    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
    assertEquals(response.getMediaType(), MediaType.APPLICATION_JSON_TYPE);
    assertTrue(response.hasEntity());
    assertTrue(response.getEntity() instanceof ApiError);
    final ApiError responseEntity = (ApiError) response.getEntity();
    assertTrue(responseEntity.getMessage().startsWith("Boom!"));
}
Also used : JsonProcessingExceptionMapper(org.graylog2.shared.rest.exceptionmappers.JsonProcessingExceptionMapper) Response(javax.ws.rs.core.Response) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ApiError(org.graylog2.plugin.rest.ApiError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Aggregations

ApiError (org.graylog2.plugin.rest.ApiError)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 Response (javax.ws.rs.core.Response)1 JsonProcessingExceptionMapper (org.graylog2.shared.rest.exceptionmappers.JsonProcessingExceptionMapper)1 Test (org.junit.Test)1