Skip to content

Commit 3bbfe41

Browse files
committed
Refactoring to eventually retain location information for all/most DatabindExceptions
1 parent 1d11481 commit 3bbfe41

12 files changed

+68
-44
lines changed

src/main/java/tools/jackson/databind/DatabindException.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ public static DatabindException from(DeserializationContext ctxt, String msg) {
7777
// // "Overrides": methods with name same as ones from JacksonException
7878
// // (but for static methods no real overriding, static dispatch)
7979

80-
public static JacksonException wrapWithPath(Throwable src, Object refFrom,
81-
String refPropertyName) {
82-
return wrapWithPath(src, new Reference(refFrom, refPropertyName));
83-
}
84-
85-
public static JacksonException wrapWithPath(Throwable src, Object refFrom, int index) {
86-
return wrapWithPath(src, new Reference(refFrom, index));
80+
public static JacksonException wrapWithPath(DeserializationContext ctxt,
81+
Throwable src, Reference ref) {
82+
// !!! TODO: get `JsonParser`, location from `ctxt`
83+
return JacksonException.wrapWithPath(src, ref,
84+
(msg, cause) -> new DatabindException(msg, null, cause));
8785
}
88-
89-
public static JacksonException wrapWithPath(Throwable src, Reference ref) {
86+
87+
public static JacksonException wrapWithPath(SerializationContext ctxt,
88+
Throwable src, Reference ref) {
89+
// !!! TODO: get `JsonGenerator` from `ctxt`
9090
return JacksonException.wrapWithPath(src, ref,
9191
(msg, cause) -> new DatabindException(msg, null, cause));
9292
}

src/main/java/tools/jackson/databind/deser/bean/BeanDeserializerBase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,16 +1826,15 @@ protected ValueDeserializer<Object> _findSubclassDeserializer(DeserializationCon
18261826
/**
18271827
* Method that will modify caught exception (passed in as argument)
18281828
* as necessary to include reference information, and to ensure it
1829-
* is a subtype of {@link IOException}, or an unchecked exception.
1829+
* is a subtype of {@link DatabindException}, or an unchecked exception.
18301830
*<p>
18311831
* Rules for wrapping and unwrapping are bit complicated; essentially:
18321832
*<ul>
18331833
* <li>Errors are to be passed as is (if uncovered via unwrapping)
1834-
* <li>"Plain" IOExceptions (ones that are not of type
1835-
* {@link DatabindException} are to be passed as is
1834+
* <li>{@code JacksonException} are to be passed as is
18361835
*</ul>
18371836
* The method always throws but declares its return type as
1838-
* {@link IOException} in order to allow callers to invoke method as
1837+
* {@link DatabindException} in order to allow callers to invoke method as
18391838
* {@code throw wrapAndThrow(...);} thereby ensuring complete code
18401839
* coverage is possible. This also ensures that all call paths within
18411840
* this method throw an exception; otherwise they would be required
@@ -1849,8 +1848,9 @@ public DatabindException wrapAndThrow(Throwable t, Object bean, String fieldName
18491848
if (fieldName == null) {
18501849
fieldName = "";
18511850
}
1852-
throw DatabindException.wrapWithPath(throwOrReturnThrowable(ctxt, t),
1853-
bean, fieldName);
1851+
throw DatabindException.wrapWithPath(ctxt,
1852+
throwOrReturnThrowable(ctxt, t),
1853+
new JacksonException.Reference(bean, fieldName));
18541854
}
18551855

18561856
private Throwable throwOrReturnThrowable(DeserializationContext ctxt, Throwable t)

src/main/java/tools/jackson/databind/deser/jdk/CollectionDeserializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ protected Collection<Object> _deserializeFromArray(JsonParser p, Deserialization
367367
if (!wrap) {
368368
ClassUtil.throwIfRTE(e);
369369
}
370-
throw DatabindException.wrapWithPath(e, result, result.size());
370+
throw DatabindException.wrapWithPath(ctxt, e,
371+
new JacksonException.Reference(result, result.size()));
371372
}
372373
}
373374
return result;
@@ -418,7 +419,8 @@ protected final Collection<Object> handleNonArray(JsonParser p, DeserializationC
418419
ClassUtil.throwIfRTE(e);
419420
}
420421
// note: pass Object.class, not Object[].class, as we need element type for error info
421-
throw DatabindException.wrapWithPath(e, Object.class, result.size());
422+
throw DatabindException.wrapWithPath(ctxt, e,
423+
new JacksonException.Reference(Object.class, result.size()));
422424
}
423425
result.add(value);
424426
return result;
@@ -466,7 +468,8 @@ protected Collection<Object> _deserializeWithObjectId(JsonParser p, Deserializat
466468
if (!wrap) {
467469
ClassUtil.throwIfRTE(e);
468470
}
469-
throw DatabindException.wrapWithPath(e, result, result.size());
471+
throw DatabindException.wrapWithPath(ctxt, e,
472+
new JacksonException.Reference(result, result.size()));
470473
}
471474
}
472475
return result;

src/main/java/tools/jackson/databind/deser/jdk/EnumSetDeserializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ protected final EnumSet<?> _deserialize(JsonParser p, DeserializationContext ctx
213213
}
214214
}
215215
} catch (Exception e) {
216-
throw DatabindException.wrapWithPath(e, result, result.size());
216+
throw DatabindException.wrapWithPath(ctxt, e,
217+
new JacksonException.Reference(result, result.size()));
217218
}
218219
return result;
219220
}
@@ -254,7 +255,8 @@ protected EnumSet<?> handleNonArray(JsonParser p, DeserializationContext ctxt,
254255
result.add(value);
255256
}
256257
} catch (Exception e) {
257-
throw DatabindException.wrapWithPath(e, result, result.size());
258+
throw DatabindException.wrapWithPath(ctxt, e,
259+
new JacksonException.Reference(result, result.size()));
258260
}
259261
return result;
260262
}

src/main/java/tools/jackson/databind/deser/jdk/FactoryBasedEnumDeserializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ protected final Object _deserializeWithErrorWrapping(JsonParser p, Deserializati
261261
protected Object wrapAndThrow(Throwable t, Object bean, String fieldName, DeserializationContext ctxt)
262262
throws DatabindException
263263
{
264-
throw DatabindException.wrapWithPath(throwOrReturnThrowable(t, ctxt), bean, fieldName);
264+
throw DatabindException.wrapWithPath(ctxt,
265+
throwOrReturnThrowable(t, ctxt),
266+
new JacksonException.Reference(bean, fieldName));
265267
}
266268

267269
private Throwable throwOrReturnThrowable(Throwable t, DeserializationContext ctxt)

src/main/java/tools/jackson/databind/deser/jdk/ObjectArrayDeserializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt)
217217
chunk[ix++] = value;
218218
}
219219
} catch (Exception e) {
220-
throw DatabindException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
220+
throw DatabindException.wrapWithPath(ctxt, e,
221+
new JacksonException.Reference(chunk, buffer.bufferedSize() + ix));
221222
}
222223

223224
Object[] result;
@@ -285,7 +286,8 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt,
285286
chunk[ix++] = value;
286287
}
287288
} catch (Exception e) {
288-
throw DatabindException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
289+
throw DatabindException.wrapWithPath(ctxt, e,
290+
new JacksonException.Reference(chunk, buffer.bufferedSize() + ix));
289291
}
290292

291293
Object[] result;

src/main/java/tools/jackson/databind/deser/jdk/PrimitiveArrayDeserializers.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ public boolean[] deserialize(JsonParser p, DeserializationContext ctxt)
397397
chunk[ix++] = value;
398398
}
399399
} catch (Exception e) {
400-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
400+
throw DatabindException.wrapWithPath(ctxt, e,
401+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
401402
}
402403
return builder.completeAndClearBuffer(chunk, ix);
403404
}
@@ -511,7 +512,8 @@ public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws Jack
511512
chunk[ix++] = value;
512513
}
513514
} catch (Exception e) {
514-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
515+
throw DatabindException.wrapWithPath(ctxt, e,
516+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
515517
}
516518
return builder.completeAndClearBuffer(chunk, ix);
517519
}
@@ -601,7 +603,8 @@ public short[] deserialize(JsonParser p, DeserializationContext ctxt) throws Jac
601603
chunk[ix++] = value;
602604
}
603605
} catch (Exception e) {
604-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
606+
throw DatabindException.wrapWithPath(ctxt, e,
607+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
605608
}
606609
return builder.completeAndClearBuffer(chunk, ix);
607610
}
@@ -677,7 +680,8 @@ public int[] deserialize(JsonParser p, DeserializationContext ctxt) throws Jacks
677680
chunk[ix++] = value;
678681
}
679682
} catch (Exception e) {
680-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
683+
throw DatabindException.wrapWithPath(ctxt, e,
684+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
681685
}
682686
return builder.completeAndClearBuffer(chunk, ix);
683687
}
@@ -753,7 +757,8 @@ public long[] deserialize(JsonParser p, DeserializationContext ctxt) throws Jack
753757
chunk[ix++] = value;
754758
}
755759
} catch (Exception e) {
756-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
760+
throw DatabindException.wrapWithPath(ctxt, e,
761+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
757762
}
758763
return builder.completeAndClearBuffer(chunk, ix);
759764
}
@@ -822,7 +827,8 @@ public float[] deserialize(JsonParser p, DeserializationContext ctxt) throws Jac
822827
chunk[ix++] = value;
823828
}
824829
} catch (Exception e) {
825-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
830+
throw DatabindException.wrapWithPath(ctxt, e,
831+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
826832
}
827833
return builder.completeAndClearBuffer(chunk, ix);
828834
}
@@ -890,7 +896,8 @@ public double[] deserialize(JsonParser p, DeserializationContext ctxt) throws Ja
890896
chunk[ix++] = value;
891897
}
892898
} catch (Exception e) {
893-
throw DatabindException.wrapWithPath(e, chunk, builder.bufferedSize() + ix);
899+
throw DatabindException.wrapWithPath(ctxt, e,
900+
new JacksonException.Reference(chunk, builder.bufferedSize() + ix));
894901
}
895902
return builder.completeAndClearBuffer(chunk, ix);
896903
}

src/main/java/tools/jackson/databind/deser/jdk/StringArrayDeserializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public String[] deserialize(JsonParser p, DeserializationContext ctxt) throws Ja
164164
chunk[ix++] = value;
165165
}
166166
} catch (Exception e) {
167-
throw DatabindException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
167+
throw DatabindException.wrapWithPath(ctxt, e,
168+
new JacksonException.Reference(chunk, buffer.bufferedSize() + ix));
168169
}
169170
String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
170171
ctxt.returnObjectBuffer(buffer);
@@ -224,7 +225,8 @@ protected final String[] _deserializeCustom(JsonParser p, DeserializationContext
224225
}
225226
} catch (Exception e) {
226227
// note: pass String.class, not String[].class, as we need element type for error info
227-
throw DatabindException.wrapWithPath(e, String.class, ix);
228+
throw DatabindException.wrapWithPath(ctxt, e,
229+
new JacksonException.Reference(String.class, ix));
228230
}
229231
String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
230232
ctxt.returnObjectBuffer(buffer);
@@ -284,7 +286,8 @@ public String[] deserialize(JsonParser p, DeserializationContext ctxt,
284286
chunk[ix++] = value;
285287
}
286288
} catch (Exception e) {
287-
throw DatabindException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
289+
throw DatabindException.wrapWithPath(ctxt, e,
290+
new JacksonException.Reference(chunk, buffer.bufferedSize() + ix));
288291
}
289292
String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
290293
ctxt.returnObjectBuffer(buffer);

src/main/java/tools/jackson/databind/deser/jdk/StringCollectionDeserializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ public Collection<String> deserialize(JsonParser p, DeserializationContext ctxt,
215215
result.add(value);
216216
}
217217
} catch (Exception e) {
218-
throw DatabindException.wrapWithPath(e, result, result.size());
218+
throw DatabindException.wrapWithPath(ctxt, e,
219+
new JacksonException.Reference(result, result.size()));
219220
}
220221
return result;
221222
}
@@ -251,7 +252,8 @@ private Collection<String> deserializeUsingCustom(JsonParser p, DeserializationC
251252
result.add(value);
252253
}
253254
} catch (Exception e) {
254-
throw DatabindException.wrapWithPath(e, result, result.size());
255+
throw DatabindException.wrapWithPath(ctxt, e,
256+
new JacksonException.Reference(result, result.size()));
255257
}
256258
return result;
257259
}

src/main/java/tools/jackson/databind/deser/std/ContainerDeserializerBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected <BOGUS> BOGUS wrapAndThrow(DeserializationContext ctxt,
155155
ClassUtil.throwIfRTE(t);
156156
}
157157
// for [databind#1141]
158-
throw DatabindException.wrapWithPath(t, ref,
159-
ClassUtil.nonNull(key, "N/A"));
158+
throw DatabindException.wrapWithPath(ctxt, t,
159+
new JacksonException.Reference(ref, ClassUtil.nonNull(key, "N/A")));
160160
}
161161
}

src/main/java/tools/jackson/databind/ser/jackson/JsonValueSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ protected boolean _acceptJsonFormatVisitorForEnum(JsonFormatVisitorWrapper visit
369369
t = t.getCause();
370370
}
371371
ClassUtil.throwIfError(t);
372-
throw DatabindException.wrapWithPath(t, en, _accessor.getName() + "()");
372+
throw DatabindException.wrapWithPath(visitor.getContext(), t,
373+
new JacksonException.Reference(en, _accessor.getName() + "()"));
373374
}
374375
}
375376
stringVisitor.enumTypes(enums);

src/main/java/tools/jackson/databind/ser/std/StdSerializer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protected void visitArrayFormat(JsonFormatVisitorWrapper visitor, JavaType typeH
249249
* <li>Wrapped {@code IOException}s are unpeeled
250250
*</ul>
251251
*/
252-
public void wrapAndThrow(SerializationContext provider,
252+
public void wrapAndThrow(SerializationContext ctxt,
253253
Throwable t, Object bean, String fieldName)
254254
throws JacksonException
255255
{
@@ -266,16 +266,17 @@ public void wrapAndThrow(SerializationContext provider,
266266
// Errors to be passed as is, most others not
267267
ClassUtil.throwIfError(t);
268268
if (!(t instanceof JacksonException)) {
269-
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
269+
boolean wrap = (ctxt == null) || ctxt.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
270270
if (!wrap) {
271271
ClassUtil.throwIfRTE(t);
272272
}
273273
}
274274
// Need to add reference information
275-
throw DatabindException.wrapWithPath(t, bean, fieldName);
275+
throw DatabindException.wrapWithPath(ctxt, t,
276+
new JacksonException.Reference(bean, fieldName));
276277
}
277278

278-
public void wrapAndThrow(SerializationContext provider,
279+
public void wrapAndThrow(SerializationContext ctxt,
279280
Throwable t, Object bean, int index)
280281
throws JacksonException
281282
{
@@ -288,13 +289,14 @@ public void wrapAndThrow(SerializationContext provider,
288289
// Errors to be passed as is, most others not
289290
ClassUtil.throwIfError(t);
290291
if (!(t instanceof JacksonException)) {
291-
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
292+
boolean wrap = (ctxt == null) || ctxt.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
292293
if (!wrap) {
293294
ClassUtil.throwIfRTE(t);
294295
}
295296
}
296297
// Need to add reference information
297-
throw DatabindException.wrapWithPath(t, bean, index);
298+
throw DatabindException.wrapWithPath(ctxt, t,
299+
new JacksonException.Reference(bean, index));
298300
}
299301

300302
/*

0 commit comments

Comments
 (0)