use of com.fasterxml.jackson.databind.exc.InvalidFormatException in project dropwizard by dropwizard.
the class JsonProcessingExceptionMapper method toResponse.
@Override
public Response toResponse(JsonProcessingException exception) {
/*
* If the error is in the JSON generation, it's a server error.
*/
if (exception instanceof JsonGenerationException) {
// LoggingExceptionMapper will log exception
return super.toResponse(exception);
}
final String message = exception.getOriginalMessage();
/*
* If we can't deserialize the JSON because someone forgot a no-arg
* constructor, or it is not known how to serialize the type it's
* a server error and we should inform the developer.
*/
if (exception instanceof JsonMappingException) {
final JsonMappingException ex = (JsonMappingException) exception;
final Throwable cause = Throwables.getRootCause(ex);
// Exceptions that denote an error on the client side
final boolean clientCause = cause instanceof InvalidFormatException || cause instanceof PropertyBindingException;
// Until completely foolproof mechanism can be worked out in coordination
// with Jackson on how to communicate client vs server fault, compare
// start of message with known server faults.
final boolean beanError = cause.getMessage() == null || (cause.getMessage().startsWith("No suitable constructor found") || cause.getMessage().startsWith("No serializer found for class") || (cause.getMessage().startsWith("Can not construct instance") && !WRONG_TYPE_REGEX.matcher(cause.getMessage()).find()));
if (beanError && !clientCause) {
// LoggingExceptionMapper will log exception
return super.toResponse(exception);
}
}
/*
* Otherwise, it's those pesky users.
*/
LOGGER.debug("Unable to process JSON", exception);
final ErrorMessage errorMessage = new ErrorMessage(Response.Status.BAD_REQUEST.getStatusCode(), "Unable to process JSON", showDetails ? message : null);
return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(errorMessage).build();
}
use of com.fasterxml.jackson.databind.exc.InvalidFormatException in project dropwizard by dropwizard.
the class BaseConfigurationFactory method build.
protected T build(JsonNode node, String path) throws IOException, ConfigurationException {
for (Map.Entry<Object, Object> pref : System.getProperties().entrySet()) {
final String prefName = (String) pref.getKey();
if (prefName.startsWith(propertyPrefix)) {
final String configName = prefName.substring(propertyPrefix.length());
addOverride(node, configName, System.getProperty(prefName));
}
}
try {
final T config = mapper.readValue(new TreeTraversingParser(node), klass);
validate(path, config);
return config;
} catch (UnrecognizedPropertyException e) {
final List<String> properties = e.getKnownPropertyIds().stream().map(Object::toString).collect(Collectors.toList());
throw ConfigurationParsingException.builder("Unrecognized field").setFieldPath(e.getPath()).setLocation(e.getLocation()).addSuggestions(properties).setSuggestionBase(e.getPropertyName()).setCause(e).build(path);
} catch (InvalidFormatException e) {
final String sourceType = e.getValue().getClass().getSimpleName();
final String targetType = e.getTargetType().getSimpleName();
throw ConfigurationParsingException.builder("Incorrect type of value").setDetail("is of type: " + sourceType + ", expected: " + targetType).setLocation(e.getLocation()).setFieldPath(e.getPath()).setCause(e).build(path);
} catch (JsonMappingException e) {
throw ConfigurationParsingException.builder("Failed to parse configuration").setDetail(e.getMessage()).setFieldPath(e.getPath()).setLocation(e.getLocation()).setCause(e).build(path);
}
}
use of com.fasterxml.jackson.databind.exc.InvalidFormatException in project jackson-databind by FasterXML.
the class JDKStringLikeTypesTest method testURL.
public void testURL() throws Exception {
URL exp = new URL("http://foo.com");
assertEquals(exp, MAPPER.readValue("\"" + exp.toString() + "\"", URL.class));
// trivial case; null to null, embedded URL to URL
TokenBuffer buf = new TokenBuffer(null, false);
buf.writeObject(null);
assertNull(MAPPER.readValue(buf.asParser(), URL.class));
buf.close();
// then, URLitself come as is:
buf = new TokenBuffer(null, false);
buf.writeObject(exp);
assertSame(exp, MAPPER.readValue(buf.asParser(), URL.class));
buf.close();
// and finally, invalid URL should be handled appropriately too
try {
URL result = MAPPER.readValue(quote("a b"), URL.class);
fail("Should not accept malformed URI, instead got: " + result);
} catch (InvalidFormatException e) {
verifyException(e, "not a valid textual representation");
}
}
use of com.fasterxml.jackson.databind.exc.InvalidFormatException in project jackson-databind by FasterXML.
the class EnumAltIdTest method testFailWhenCaseSensitiveAndToStringIsUpperCase.
public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException {
ObjectReader r = READER_DEFAULT.forType(LowerCaseEnum.class).with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
try {
r.readValue("\"A\"");
fail("InvalidFormatException expected");
} catch (InvalidFormatException e) {
verifyException(e, "value not one of declared Enum instance names: [a, b, c]");
}
}
use of com.fasterxml.jackson.databind.exc.InvalidFormatException in project jackson-databind by FasterXML.
the class TestTreeTraversingParser method testTextAsBinary.
public void testTextAsBinary() throws Exception {
TextNode n = new TextNode(" APs=\n");
JsonParser p = n.traverse();
assertNull(p.getCurrentToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
byte[] data = p.getBinaryValue();
assertNotNull(data);
assertArrayEquals(new byte[] { 0, -5 }, data);
assertNull(p.nextToken());
p.close();
assertTrue(p.isClosed());
// Also: let's verify we get an exception for garbage...
n = new TextNode("?!??");
p = n.traverse();
assertToken(JsonToken.VALUE_STRING, p.nextToken());
try {
p.getBinaryValue();
} catch (InvalidFormatException e) {
verifyException(e, "Illegal character");
}
p.close();
}
Aggregations