Skip to content

Commit

Permalink
More rerouting of exceptions through DeserializationContext.reportXxx…
Browse files Browse the repository at this point in the history
…() instead of direct throws
  • Loading branch information
cowtowncoder committed May 10, 2016
1 parent 4b9bfd0 commit 48437a7
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 60 deletions.
Expand Up @@ -852,18 +852,6 @@ public void reportUnknownProperty(Object instanceOrClass, String fieldName,
instanceOrClass, fieldName, propIds);
}

/**
* @since 2.8
*/
public void reportMappingException(String msg, Object... msgArgs)
throws JsonMappingException
{
if (msgArgs.length > 0) {
msg = String.format(msg, msgArgs);
}
throw mappingException(msg);
}

/**
* @since 2.8
*/
Expand Down Expand Up @@ -954,6 +942,32 @@ public void reportEndOfInputException(Class<?> instClass) throws JsonMappingExce
throw endOfInputException(instClass);
}

/**
* @since 2.8
*/
public void reportMappingException(String msg, Object... msgArgs)
throws JsonMappingException
{
if (msgArgs.length > 0) {
msg = String.format(msg, msgArgs);
}
throw mappingException(msg);
}

/**
* @since 2.8
*/
public void reportMappingException(Class<?> targetClass, JsonToken t) throws JsonMappingException {
throw mappingException(targetClass, t);
}

/**
* @since 2.8
*/
public void reportMappingException(Class<?> targetClass) throws JsonMappingException {
throw mappingException(targetClass);
}

/*
/**********************************************************
/* Methods for constructing exceptions, "untyped"
Expand All @@ -967,16 +981,20 @@ public JsonMappingException mappingException(Class<?> targetClass) {
return mappingException(targetClass, _parser.getCurrentToken());
}

/**
* @deprecated Since 2.8 use {@link #reportMappingException(String, Object...)} instead
*/
@Deprecated
public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) {
return JsonMappingException.from(_parser,
String.format("Can not deserialize instance of %s out of %s token",
_calcName(targetClass), token));
}

/**
* Helper method for constructing generic mapping exception with specified
* message and current location information
* @deprecated Since 2.8 use {@link #reportMappingException(String, Object...)} instead
*/
@Deprecated
public JsonMappingException mappingException(String message) {
return JsonMappingException.from(getParser(), message);
}
Expand Down
Expand Up @@ -168,7 +168,8 @@ protected final Object _deserializeOther(JsonParser p, DeserializationContext ct
return deserializeFromObject(p, ctxt);
default:
}
throw ctxt.mappingException(handledType());
ctxt.reportMappingException(handledType());
return null;
}

protected Object _missingToken(JsonParser p, DeserializationContext ctxt) throws IOException {
Expand Down Expand Up @@ -691,7 +692,8 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, Deseri
// !!! 08-Jul-2011, tatu: Could probably support; but for now
// it's too complicated, so bail out
tokens.close();
throw ctxt.mappingException("Can not create polymorphic instances with unwrapped values");
ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
return null;
}
return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
}
Expand Down Expand Up @@ -856,7 +858,8 @@ protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p, D
if (bean.getClass() != _beanType.getRawClass()) {
// !!! 08-Jul-2011, tatu: Could theoretically support; but for now
// it's too complicated, so bail out
throw ctxt.mappingException("Can not create polymorphic instances with unwrapped values");
ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
return null;
}
return ext.complete(p, ctxt, bean);
}
Expand Down
Expand Up @@ -1299,7 +1299,8 @@ public Object deserializeFromArray(JsonParser p, DeserializationContext ctxt) th
if (t == JsonToken.END_ARRAY) {
return null;
}
throw ctxt.mappingException(handledType(), JsonToken.START_ARRAY);
ctxt.reportMappingException(handledType(), JsonToken.START_ARRAY);
return null;
}
throw ctxt.mappingException(handledType());
}
Expand Down
Expand Up @@ -590,7 +590,8 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p,
if (bean.getClass() != _beanType.getRawClass()) {
// !!! 08-Jul-2011, tatu: Could probably support; but for now
// it's too complicated, so bail out
throw ctxt.mappingException("Can not create polymorphic instances with unwrapped values");
ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
return null;
}
return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
}
Expand Down
Expand Up @@ -24,6 +24,7 @@ public FailingDeserializer(String m) {

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws JsonMappingException{
throw ctxt.mappingException(_message);
ctxt.reportMappingException(_message);
return null;
}
}
Expand Up @@ -212,7 +212,8 @@ protected final ObjectNode deserializeObject(JsonParser p, DeserializationContex
return node;
}
if (t != JsonToken.FIELD_NAME) {
throw ctxt.mappingException(handledType(), p.getCurrentToken());
ctxt.reportMappingException(handledType(), p.getCurrentToken());
return null;
}
key = p.getCurrentName();
}
Expand Down Expand Up @@ -263,7 +264,8 @@ protected final ArrayNode deserializeArray(JsonParser p, DeserializationContext
while (true) {
JsonToken t = p.nextToken();
if (t == null) {
throw ctxt.mappingException("Unexpected end-of-input when binding data into ArrayNode");
ctxt.reportEndOfInputException(ArrayNode.class);
return node;
}
switch (t.id()) {
case JsonTokenId.ID_START_OBJECT:
Expand Down
Expand Up @@ -426,7 +426,8 @@ protected final void _readAndBind(JsonParser p, DeserializationContext ctxt,
return;
}
if (t != JsonToken.FIELD_NAME) {
throw ctxt.mappingException(_mapType.getRawClass(), p.getCurrentToken());
ctxt.reportMappingException(_mapType.getRawClass(), p.getCurrentToken());
return;
}
keyStr = p.getCurrentName();
}
Expand Down Expand Up @@ -487,7 +488,8 @@ protected final void _readAndBindStringMap(JsonParser p, DeserializationContext
return;
}
if (t != JsonToken.FIELD_NAME) {
throw ctxt.mappingException(_mapType.getRawClass(), p.getCurrentToken());
ctxt.reportMappingException(_mapType.getRawClass(), p.getCurrentToken());
return;
}
key = p.getCurrentName();
}
Expand Down
Expand Up @@ -173,7 +173,7 @@ public Map.Entry<Object,Object> deserialize(JsonParser jp, DeserializationContex
// Ok: must point to START_OBJECT, FIELD_NAME or END_OBJECT
JsonToken t = jp.getCurrentToken();
if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
// [JACKSON-620] (empty) String may be ok however:
// String may be ok however:
// slightly redundant (since String was passed above), but
return _deserializeFromEmpty(jp, ctxt);
}
Expand All @@ -182,9 +182,11 @@ public Map.Entry<Object,Object> deserialize(JsonParser jp, DeserializationContex
}
if (t != JsonToken.FIELD_NAME) {
if (t == JsonToken.END_OBJECT) {
throw ctxt.mappingException("Can not deserialize a Map.Entry out of empty JSON Object");
ctxt.reportMappingException("Can not deserialize a Map.Entry out of empty JSON Object");
return null;
}
throw ctxt.mappingException(handledType(), t);
ctxt.reportMappingException(handledType(), t);
return null;
}

final KeyDeserializer keyDes = _keyDeserializer;
Expand Down Expand Up @@ -213,10 +215,12 @@ public Map.Entry<Object,Object> deserialize(JsonParser jp, DeserializationContex
t = jp.nextToken();
if (t != JsonToken.END_OBJECT) {
if (t == JsonToken.FIELD_NAME) { // most likely
throw ctxt.mappingException("Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '"+jp.getCurrentName()+"')");
ctxt.reportMappingException("Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '"+jp.getCurrentName()+"')");
} else {
// how would this occur?
ctxt.reportMappingException("Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: "+t);
}
// how would this occur?
throw ctxt.mappingException("Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: "+t);
return null;
}
return new AbstractMap.SimpleEntry<Object,Object>(key, value);
}
Expand Down
Expand Up @@ -284,7 +284,8 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt)
return C;
}
}
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
return null;
}
}

Expand Down Expand Up @@ -490,7 +491,8 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
break;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
return null;
}

/**
Expand Down Expand Up @@ -576,7 +578,8 @@ public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws
}
}
// String is ok too, can easily convert; otherwise, no can do:
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
return null;
}
}

Expand Down Expand Up @@ -620,7 +623,8 @@ public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt)
break;
}
// Otherwise, no can do:
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
return null;
}
}
}
Expand Up @@ -54,6 +54,7 @@ public StackTraceElement deserialize(JsonParser p, DeserializationContext ctxt)
}
return value;
}
throw ctxt.mappingException(_valueClass, t);
ctxt.reportMappingException(_valueClass, t);
return null;
}
}

0 comments on commit 48437a7

Please sign in to comment.