Java Code Examples for com.fasterxml.jackson.core.JsonProcessingException#getLocation()

The following examples show how to use com.fasterxml.jackson.core.JsonProcessingException#getLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TitusExceptionMapper.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private Response fromJsonProcessingException(JsonProcessingException e) {
    StringBuilder msgBuilder = new StringBuilder();
    if (e.getOriginalMessage() != null) {
        msgBuilder.append(e.getOriginalMessage());
    } else {
        msgBuilder.append("JSON processing error");
    }
    JsonLocation location = e.getLocation();
    if (location != null) {
        msgBuilder.append(" location: [line: ").append(location.getLineNr())
                .append(", column: ").append(location.getColumnNr()).append(']');
    }

    ErrorResponse errorResponse = ErrorResponse.newError(HttpServletResponse.SC_BAD_REQUEST, msgBuilder.toString())
            .clientRequest(httpServletRequest)
            .serverContext()
            .exceptionContext(e)
            .build();
    return Response.status(Status.BAD_REQUEST).entity(errorResponse).build();
}
 
Example 2
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private static Map<String,Object> readFragment(File file, String ext) throws IOException {
    ObjectMapper mapper = new ObjectMapper("json".equals(ext) ? new JsonFactory() : new YAMLFactory());
    TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
    try {
        Map<String, Object> ret = mapper.readValue(file, typeRef);
        return ret != null ? ret : new HashMap<>();
    } catch (JsonProcessingException e) {
        throw new JsonMappingException(String.format("[%s] %s", file, e.getMessage()), e.getLocation(), e);
    }
}
 
Example 3
Source File: ErrorResponses.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> appendJacksonErrorDetails(JsonProcessingException cause) {
    Map<String, Object> out = new TreeMap<>();

    JsonLocation location = cause.getLocation();
    if (location != null) {
        out.put("errorLocation", "line: " + location.getLineNr() + ", column: " + location.getColumnNr());
        if (location.getSourceRef() != null && location.getSourceRef() instanceof String) {
            out.put("document", location.getSourceRef());
        }
    }

    if (cause instanceof JsonMappingException) {
        JsonMappingException mappingEx = (JsonMappingException) cause;
        if (mappingEx.getPathReference() != null) {
            out.put("pathReference", mappingEx.getPathReference());
        }

        if (cause instanceof InvalidFormatException) {
            InvalidFormatException formEx = (InvalidFormatException) cause;
            if (formEx.getTargetType() != null) {
                out.put("targetType", formEx.getTargetType().getName());
            }
        } else if (cause instanceof PropertyBindingException) {
            PropertyBindingException bindingEx = (PropertyBindingException) cause;
            if (bindingEx.getPropertyName() != null) {
                out.put("property", bindingEx.getPropertyName());
                out.put("knownProperties", bindingEx.getKnownPropertyIds());
            }
        }
    }

    return out;
}
 
Example 4
Source File: ParseError.java    From json-schema-validator-demo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static JsonNode build(final JsonProcessingException e,
    final boolean crlf)
{
    final JsonLocation location = e.getLocation();
    final ObjectNode ret = JsonNodeFactory.instance.objectNode();

    /*
     * Unfortunately, for some reason, Jackson botches the column number in
     * its JsonPosition -- I cannot figure out why exactly. However, it does
     * have a correct offset into the buffer.
     *
     * The problem is that if the input has CR/LF line terminators, its
     * offset will be "off" by the number of lines minus 1 with regards to
     * what JavaScript sees as positions in text areas. Make the necessary
     * adjustments so that the caret jumps at the correct position in this
     * case.
     */
    final int lineNr = location.getLineNr();
    int offset = (int) location.getCharOffset();
    if (crlf)
        offset = offset - lineNr + 1;
    ret.put(LINE, lineNr);
    ret.put(OFFSET, offset);

    // Finally, put the message
    ret.put(MESSAGE, e.getOriginalMessage());
    return ret;
}
 
Example 5
Source File: JsonReadException.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static JsonReadException fromJackson(JsonProcessingException ex)
{
    String message = ex.getMessage();

    // Strip off location.
    int locPos = message.lastIndexOf(" at [Source");
    if (locPos >= 0) {
        message = message.substring(0, locPos);
    }

    return new JsonReadException(message, ex.getLocation());
}
 
Example 6
Source File: MalformedJsonException.java    From bonita-ui-designer with GNU General Public License v2.0 4 votes vote down vote up
public MalformedJsonException(JsonProcessingException cause) {
    super(cause);
    this.location = cause.getLocation();
}
 
Example 7
Source File: SwaggerError.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 4 votes vote down vote up
public static SwaggerError newJsonError(JsonProcessingException exception) {
    int line = (exception.getLocation() != null) ? exception.getLocation().getLineNr() : 1;
    return new SwaggerError(line, IMarker.SEVERITY_ERROR, 0, exception.getMessage());
}
 
Example 8
Source File: JsonProcessingExceptionMapper.java    From ameba with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Response toResponse(JsonProcessingException exception) {
    Throwable throwable = exception;
    while (throwable != null) {
        if (throwable instanceof PersistenceException) {
            return exceptionMappers.get().findMapping(throwable).toResponse(throwable);
        }
        throwable = throwable.getCause();
    }

    logger.debug("Json Processing error", exception);
    String message = exception.getOriginalMessage();
    String desc = null;
    String source = null;
    if (mode.isDev()) {
        desc = IOUtils.getStackTrace(exception);
        JsonLocation location = exception.getLocation();
        if (location != null) {
            source = "line: " + location.getLineNr() +
                    ", column: " + location.getColumnNr();
        } else {
            source = exception.getStackTrace()[0].toString();
        }
    }

    ErrorMessage errorMessage = ErrorMessage.fromStatus(Response.Status.BAD_REQUEST.getStatusCode());
    errorMessage.setThrowable(exception);
    errorMessage.setCode(Hashing.murmur3_32().hashUnencodedChars(exception.getClass().getName()).toString());

    errorMessage.addError(new Result.Error(
            errorMessage.getCode(),
            message != null ? message : exception.getMessage(),
            desc,
            source
    ));

    return Response.status(errorMessage.getStatus())
            .entity(errorMessage)
            .type(ExceptionMapperUtils.getResponseType())
            .build();
}