throw ConfigurationParsingException.builder("Failed to parse configuration") .setDetail(e.getMessage()) .setFieldPath(e.getPath()) .setLocation(e.getLocation()) .setCause(e)
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseBody public String handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) { JsonMappingException jme = (JsonMappingException) ex.getCause(); return jme.getPath().get(0).getFieldName() + " invalid"; }
private static List<String> getReferences(JsonMappingException e) { List<JsonMappingException.Reference> references = e.getPath(); List<String> ret = new LinkedList<>(); if (references != null) { for (JsonMappingException.Reference reference : references) { ret.add(reference.getFieldName()); } } return ret; } }
private ValidationJsonException(final JsonMappingException mappingException, final String message, final String rule) { this(message); final StringBuilder propertyPath = buildPropertyPath(mappingException.getPath()); if (propertyPath.length() > 0) { // Add the error. final Map<String, Serializable> error = new HashMap<>(); error.put("rule", rule); errors.put(propertyPath.toString(), Collections.singletonList(error)); } }
private Object createContent(JsonMappingException exception) { Map<String, String> map = new HashMap<String, String>(); for(Reference ref : exception.getPath()) { map.put(ref.getFieldName(), process(exception.getMessage())); } return map; }
/** * Helper function to construct a string representation of a property path from a JsonMappingException * (similar to a property path from a ConstraintViolationException). * * @param exception JsonMappingException * @return String representation of property path */ static String printPropertyPath(JsonMappingException exception) { if (CollectionUtils.isEmpty(exception.getPath())) return "UNKNOWN"; String propertyPath = ""; for (JsonMappingException.Reference ref : exception.getPath()) { if (ref.getFieldName() != null) { if (!propertyPath.isEmpty()) { propertyPath += "."; } propertyPath += ref.getFieldName(); } else { propertyPath += String.format("[%d]", ref.getIndex()); } } return propertyPath; }
public static JsonException fromJackson(JsonMappingException e) { Throwable rootCause = getRootCause(e); String message = rootCause.getMessage(); if (rootCause instanceof PatternSyntaxException) { PatternSyntaxException patternSyntaxException = (PatternSyntaxException) rootCause; message = patternSyntaxException.getMessage(); } else if (rootCause instanceof JsonMappingException) { message = ((JsonMappingException) rootCause).getOriginalMessage(); } else if (rootCause instanceof InvalidInputException) { message = ((InvalidInputException) rootCause).getErrors().first().getDetail(); } List<String> nodes = transform(e.getPath(), TO_NODE_NAMES); String pointer = '/' + Joiner.on('/').join(nodes); return new JsonException(Errors.single(10, pointer, "Error parsing JSON", message)); }
@Override public Response toResponse(JsonMappingException exception) { LOGGER.log(Level.FINE, "QBECXFExceptionMapper: JSON Mapping Exception", exception); JsonMappingException jsonException = (JsonMappingException) exception; String fieldName = ""; Iterator<JsonMappingException.Reference> referenceIt = jsonException.getPath().iterator(); while (referenceIt.hasNext()) { fieldName = fieldName.concat(referenceIt.next().getFieldName()); if (referenceIt.hasNext()) { fieldName = fieldName.concat("."); } } ValidationErrors errors = new ValidationErrors(); ValidationFieldErrors vfe = new ValidationFieldErrors(fieldName); ValidationErrorType vet = new ValidationErrorType("org.codehaus.jackson.map.JsonMappingException"); vet.putAttribute(ValidationAttribute.Message, "org.codehaus.jackson.map.JsonMappingException"); vfe.addError(vet); errors.addValidationError(vfe); return Response.status(Response.Status.BAD_REQUEST) .entity(errors) .build(); } }
@Override public Set<ValidationResult> validate(ObjectNode json) { try { parser.readValue(json.toString(), UpdateMessagePatch.class); } catch (JsonMappingException e) { return ImmutableSet.of(ValidationResult.builder() .property(firstFieldFrom(e.getPath()).orElse(ValidationResult.UNDEFINED_PROPERTY)) .message(e.getMessage()) .build()); } catch (IOException e) { return ImmutableSet.of(ValidationResult.builder() .message(e.getMessage()) .build()); } return ImmutableSet.of(); }
} catch (JsonMappingException ex) { this.resultObject = (ArticlesResponse) ex.getPath().get(0).getFrom(); } catch (Exception ex) { ex.printStackTrace();
throw ConfigurationParsingException.builder("Failed to parse configuration") .setDetail(e.getMessage()) .setFieldPath(e.getPath()) .setLocation(e.getLocation()) .setCause(e)
throw ConfigurationParsingException.builder("Failed to parse configuration") .setDetail(e.getMessage()) .setFieldPath(e.getPath()) .setLocation(e.getLocation()) .setCause(e)