Example usage for com.fasterxml.jackson.databind JsonMappingException getMessage

List of usage examples for com.fasterxml.jackson.databind JsonMappingException getMessage

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonMappingException getMessage.

Prototype

public String getMessage() 

Source Link

Usage

From source file:org.midonet.cluster.rest_api.serialization.MidonetObjectMapper.java

static WebApplicationException wrapException(JsonMappingException e, DeserializationConfig config,
        JavaType valueType) {//w  ww . j  av  a  2 s .co  m
    if (e.getPath().size() == 0)
        return new BadRequestHttpException(e.getMessage());
    JsonMappingException.Reference ref = e.getPath().get(0);
    if (null == ref || null == ref.getFrom())
        return new BadRequestHttpException(e.getMessage());

    JsonError error = getError(config, valueType, ref.getFieldName());
    if (null == error)
        return new BadRequestHttpException(e.getMessage());

    String message;
    if (StringUtils.isNotEmpty(error.message())) {
        message = MessageProperty.getMessage(error.message());
    } else {
        message = error.value();
    }
    return new WebApplicationException(ResponseUtils.buildErrorResponse(error.status(), message));
}

From source file:com.erudika.para.rest.RestUtils.java

/**
 * Returns a Response with the entity object inside it and 200 status code.
 * If there was and error the status code is different than 200.
 * @param is the entity input stream//from w  w w.  j  a  v  a2s  .  c o  m
 * @param type the type to convert the entity into, for example a Map.
 * @return response with 200 or error status
 */
public static Response getEntity(InputStream is, Class<?> type) {
    Object entity;
    try {
        if (is != null && is.available() > 0) {
            if (is.available() > Config.MAX_ENTITY_SIZE_BYTES) {
                return getStatusResponse(Response.Status.BAD_REQUEST, "Request is too large - the maximum is "
                        + (Config.MAX_ENTITY_SIZE_BYTES / 1024) + " KB.");
            }
            entity = ParaObjectUtils.getJsonReader(type).readValue(is);
        } else {
            return getStatusResponse(Response.Status.BAD_REQUEST, "Missing request body.");
        }
    } catch (JsonMappingException e) {
        return getStatusResponse(Response.Status.BAD_REQUEST, e.getMessage());
    } catch (JsonParseException e) {
        return getStatusResponse(Response.Status.BAD_REQUEST, e.getMessage());
    } catch (IOException e) {
        logger.error(null, e);
        return getStatusResponse(Response.Status.INTERNAL_SERVER_ERROR, e.toString());
    }

    return Response.ok(entity).build();
}

From source file:se.bitcraze.crazyflielib.bootloader.Bootloader.java

public static Manifest readManifest(File file) throws IOException {
    String errorMessage = "";
    try {/*  w w  w. j  a va 2  s. co  m*/
        Manifest readValue = mMapper.readValue(file, Manifest.class);
        return readValue;
    } catch (JsonParseException jpe) {
        errorMessage = jpe.getMessage();
    } catch (JsonMappingException jme) {
        errorMessage = jme.getMessage();
    }
    LoggerFactory.getLogger("Bootloader")
            .error("Error while parsing manifest " + file.getName() + ": " + errorMessage);
    return null;
}

From source file:se.bitcraze.crazyflielib.bootloader.Bootloader.java

public static void writeManifest(String fileName, Manifest manifest) throws IOException {
    String errorMessage = "";
    mMapper.enable(SerializationFeature.INDENT_OUTPUT);
    try {//from w  w w  . j  a  va 2  s.c  o m
        mMapper.writeValue(new File(fileName), manifest);
        return;
    } catch (JsonGenerationException jge) {
        errorMessage = jge.getMessage();
    } catch (JsonMappingException jme) {
        errorMessage = jme.getMessage();
    }
    LoggerFactory.getLogger("Bootloader")
            .error("Could not save manifest to file " + fileName + ".\n" + errorMessage);
}

From source file:rapture.server.ArgumentParser.java

/**
 * Having to do this kind of sucks, but we need to: the getMessage() method of JsonMappingException appends some information to the message that is useful,
 * but not very legible and exposes Java internals. We need to have as much of a language-agnostic message as possible here, since different languages will
 * be using the API and we should not expose Java errors. To do this, we need to get the plain error message that happens to be stored in the private field
 * detailMessage of the Throwable class (parent of JsonMappingException)
 *
 * @param e//  w ww .j ava  2 s  . c om
 * @return
 */
private static String getReadableMessage(JsonMappingException e) {
    Field detailMessageField = null;
    try {
        detailMessageField = Throwable.class.getDeclaredField("detailMessage");
    } catch (NoSuchFieldException e1) {
        log.error("Error getting readable message from exception: " + ExceptionToString.format(e1));
    }
    Object value = null;
    if (detailMessageField != null) {
        detailMessageField.setAccessible(true);
        try {
            value = detailMessageField.get(e);
        } catch (IllegalAccessException e1) {
            log.error("Error getting readable message from exception: " + ExceptionToString.format(e1));
        }
    }

    if (value != null) {
        return value.toString();
    } else {
        return e.getMessage(); // fallback value
    }
}

From source file:elaborate.editor.providers.JsonMappingExceptionMapper.java

@Override
public Response toResponse(JsonMappingException exception) {
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(exception.getMessage()).build();
}

From source file:com.clicktravel.cheddar.rest.exception.mapper.cdm1.JsonMappingExceptionMapper.java

@Override
public Response toResponse(final JsonMappingException exception) {
    if (logger.isDebugEnabled()) {
        logger.debug(exception.getMessage(), exception);
    }/*from  w  w w .j a v  a 2 s  .  co  m*/
    return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorResponse(exception)).build();
}

From source file:com.jim.im.exception.ImJsonMappingExceptionMapper.java

@Override
public Response toResponse(JsonMappingException e) {
    ApiErrorCode errorCode = ApiErrorCode.PARAM_ERROR;

    String errorMsg = e.getMessage();
    if (StringUtils.isBlank(errorMsg)) {
        errorMsg = IMConstant.MSG_PARAM_ERROR;
    }//from w w w.  j ava  2  s. c  o m

    String requestId = RequestContext.get(IMConstant.REQUEST_ID);

    ApiErrorCodeException errorCodeException = new ApiErrorCodeException(requestId, errorCode, errorMsg, e);
    logger.error(requestId, errorCodeException);

    return RestResult.failure(requestId, errorCode.errorCode, errorMsg);
}

From source file:nl.talsmasoftware.enumerables.support.json.jackson2.EnumerableJackson2ModuleTest.java

@Test
public void testDeserialization_emptyBrandObject() throws IOException {
    try {// w  ww  . j  a va  2s.c  o  m
        mapperWith(new EnumerableModule()).readValue(fixture("../car_with_empty_brand.json"), Car.class);
        fail("Exception expected.");
    } catch (JsonMappingException expected) {
        assertThat(expected.getMessage(),
                containsString("Attribute \"value\" is required to parse an Enumerable JSON object."));
    }
}

From source file:com.unboundid.scim2.common.ListResponseTestCase.java

/**
 * Test list response.//from  ww w.  j a  v a 2s . c  o  m
 *
 * @throws Exception If an error occurs.
 */
@Test
public void testListResponse() throws Exception {
    ListResponse<ObjectNode> listResponse = JsonUtils.getObjectReader()
            .forType(new TypeReference<ListResponse<ObjectNode>>() {
            }).readValue("{  \n" + "  \"schemas\":[  \n"
                    + "    \"urn:ietf:params:scim:api:messages:2.0:ListResponse\"\n" + "  ],\n" +
                    // Test required property case-insensitivity
                    "  \"totalresults\":2,\n" + "  \"startIndex\":1,\n" +
                    // Test case-insensitivity
                    "  \"ItemsPerPage\":3,\n" + "  \"Resources\":[  \n" + "    {  \n"
                    + "      \"userName\":\"bjensen\"\n" + "    },\n" + "    {  \n"
                    + "      \"userName\":\"jsmith\"\n" + "    }\n" + "  ]\n" + "}");

    try {
        listResponse = JsonUtils.getObjectReader().forType(new TypeReference<ListResponse<ObjectNode>>() {
        }).readValue("{  \n" + "  \"schemas\":[  \n"
                + "    \"urn:ietf:params:scim:api:messages:2.0:ListResponse\"\n" + "  ],\n" +
                // Test missing required property: totalResults
                "  \"startIndex\":1,\n" +
                // Test case-insensitivity
                "  \"ItemsPerPage\":3,\n" + "  \"Resources\":[  \n" + "    {  \n"
                + "      \"userName\":\"bjensen\"\n" + "    },\n" + "    {  \n"
                + "      \"userName\":\"jsmith\"\n" + "    }\n" + "  ]\n" + "}");
        fail("Expected failure for missing required property 'totalResults'");
    } catch (final JsonMappingException je) {
        assertTrue(je.getMessage().contains("Missing required creator property"), je.getMessage());
    }

    assertEquals(listResponse.getTotalResults(), 2);
    assertEquals(listResponse.getStartIndex(), Integer.valueOf(1));
    assertEquals(listResponse.getItemsPerPage(), Integer.valueOf(3));
    assertEquals(listResponse.getResources().size(), 2);

    ArrayList<ResourceTypeResource> resourceTypeList = new ArrayList<ResourceTypeResource>();
    resourceTypeList.add(new ResourceTypeResource("urn:test", "test", "test", new URI("/test"),
            new URI("urn:test"), Collections.<ResourceTypeResource.SchemaExtension>emptyList()));
    resourceTypeList.add(new ResourceTypeResource("urn:test2", "test2", "test2", new URI("/test2"),
            new URI("urn:test2"), Collections.<ResourceTypeResource.SchemaExtension>emptyList()));
    ListResponse<ResourceTypeResource> response = new ListResponse<ResourceTypeResource>(100, resourceTypeList,
            1, 10);

    String serialized = JsonUtils.getObjectWriter().writeValueAsString(response);
    assertEquals(JsonUtils.getObjectReader().forType(new TypeReference<ListResponse<ResourceTypeResource>>() {
    }).readValue(serialized), response);
}