Skip to content

Commit 1d11481

Browse files
committed
Minor improvement to DatabindException.wrapWithPath()
1 parent 77b3648 commit 1d11481

File tree

8 files changed

+41
-17
lines changed

8 files changed

+41
-17
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ public static DatabindException from(DeserializationContext ctxt, String msg) {
7474
return new DatabindException(_parser(ctxt), msg);
7575
}
7676

77+
// // "Overrides": methods with name same as ones from JacksonException
78+
// // (but for static methods no real overriding, static dispatch)
79+
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));
87+
}
88+
89+
public static JacksonException wrapWithPath(Throwable src, Reference ref) {
90+
return JacksonException.wrapWithPath(src, ref,
91+
(msg, cause) -> new DatabindException(msg, null, cause));
92+
}
93+
94+
/*
95+
/**********************************************************************
96+
/* Helper methods
97+
/**********************************************************************
98+
*/
99+
77100
private static JsonParser _parser(DeserializationContext ctxt) {
78101
return (ctxt == null) ? null : ctxt.getParser();
79102
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected final Object finishBuild(DeserializationContext ctxt, Object builder)
120120
try {
121121
return _buildMethod.getMember().invoke(builder, (Object[]) null);
122122
} catch (Exception e) {
123-
return wrapInstantiationProblem(e, ctxt);
123+
return wrapInstantiationProblem(ctxt, e);
124124
}
125125
}
126126

@@ -330,7 +330,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p,
330330
try {
331331
builder = creator.build(ctxt, buffer);
332332
} catch (Exception e) {
333-
return wrapInstantiationProblem(e, ctxt);
333+
return wrapInstantiationProblem(ctxt, e);
334334
}
335335
}
336336
return builder;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p, final
449449
try {
450450
bean = creator.build(ctxt, buffer);
451451
} catch (Exception e) {
452-
return wrapInstantiationProblem(e, ctxt);
452+
return wrapInstantiationProblem(ctxt, e);
453453
}
454454
}
455455
return bean;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
597597
try {
598598
bean = creator.build(ctxt, buffer);
599599
} catch (Exception e) {
600-
bean = wrapInstantiationProblem(e, ctxt);
600+
bean = wrapInstantiationProblem(ctxt, e);
601601
}
602602
// [databind#631]: Assign current value, to be accessible by custom serializers
603603
p.assignCurrentValue(bean);
@@ -687,7 +687,7 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
687687
try {
688688
bean = creator.build(ctxt, buffer);
689689
} catch (Exception e) {
690-
return wrapInstantiationProblem(e, ctxt);
690+
return wrapInstantiationProblem(ctxt, e);
691691
}
692692
p.assignCurrentValue(bean);
693693
// [databind#4938] Since 2.19, allow returning `null` from creator,
@@ -1047,7 +1047,7 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, Deseri
10471047
try {
10481048
bean = creator.build(ctxt, buffer);
10491049
} catch (Exception e) {
1050-
bean = wrapInstantiationProblem(e, ctxt);
1050+
bean = wrapInstantiationProblem(ctxt, e);
10511051
}
10521052
// [databind#631]: Assign current value, to be accessible by custom serializers
10531053
p.assignCurrentValue(bean);
@@ -1129,7 +1129,7 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, Deseri
11291129
try {
11301130
bean = creator.build(ctxt, buffer);
11311131
} catch (Exception e) {
1132-
return wrapInstantiationProblem(e, ctxt);
1132+
return wrapInstantiationProblem(ctxt, e);
11331133
}
11341134
// [databind#4938] Since 2.19, allow returning `null` from creator,
11351135
// but if so, need to skip all possibly relevant content
@@ -1309,7 +1309,7 @@ protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p, D
13091309
try {
13101310
return ext.complete(p, ctxt, buffer, creator);
13111311
} catch (Exception e) {
1312-
return wrapInstantiationProblem(e, ctxt);
1312+
return wrapInstantiationProblem(ctxt, e);
13131313
}
13141314
}
13151315

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,18 +1841,19 @@ protected ValueDeserializer<Object> _findSubclassDeserializer(DeserializationCon
18411841
* this method throw an exception; otherwise they would be required
18421842
* to return.
18431843
*/
1844-
public DatabindException wrapAndThrow(Throwable t, Object bean, String fieldName, DeserializationContext ctxt)
1844+
public DatabindException wrapAndThrow(Throwable t, Object bean, String fieldName,
1845+
DeserializationContext ctxt)
18451846
{
18461847
// 23-Aug-2022, tatu: Due to fix to prevent "double-array-wrapping", looks
18471848
// like 'fieldName' may occasionally be `null`; hence
18481849
if (fieldName == null) {
18491850
fieldName = "";
18501851
}
1851-
throw DatabindException.wrapWithPath(throwOrReturnThrowable(t, ctxt),
1852+
throw DatabindException.wrapWithPath(throwOrReturnThrowable(ctxt, t),
18521853
bean, fieldName);
18531854
}
18541855

1855-
private Throwable throwOrReturnThrowable(Throwable t, DeserializationContext ctxt)
1856+
private Throwable throwOrReturnThrowable(DeserializationContext ctxt, Throwable t)
18561857
throws JacksonException
18571858
{
18581859
// 05-Mar-2009, tatu: But one nasty edge is when we get
@@ -1875,7 +1876,7 @@ private Throwable throwOrReturnThrowable(Throwable t, DeserializationContext ctx
18751876
return t;
18761877
}
18771878

1878-
protected Object wrapInstantiationProblem(Throwable t, DeserializationContext ctxt)
1879+
protected Object wrapInstantiationProblem(DeserializationContext ctxt,Throwable t)
18791880
throws JacksonException
18801881
{
18811882
while (t instanceof InvocationTargetException && t.getCause() != null) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ protected Object finishBuild(DeserializationContext ctxt, Object builder)
228228
try {
229229
return _buildMethod.getMember().invoke(builder, (Object[]) null);
230230
} catch (Exception e) {
231-
return wrapInstantiationProblem(e, ctxt);
231+
return wrapInstantiationProblem(ctxt, e);
232232
}
233233
}
234234

@@ -473,7 +473,7 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p,
473473
try {
474474
builder = creator.build(ctxt, buffer);
475475
} catch (Exception e) {
476-
builder = wrapInstantiationProblem(e, ctxt);
476+
builder = wrapInstantiationProblem(ctxt, e);
477477
}
478478
if (unknown != null) {
479479
// polymorphic?
@@ -799,7 +799,7 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p,
799799
try {
800800
builder = creator.build(ctxt, buffer);
801801
} catch (Exception e) {
802-
return wrapInstantiationProblem(e, ctxt);
802+
return wrapInstantiationProblem(ctxt, e);
803803

804804
}
805805
return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, builder, tokens);

src/test/java/tools/jackson/databind/exc/UnwrapRootCause4603Test.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void testExceptionWrappingConfiguration()
6666
throws Exception
6767
{
6868
JacksonException disabledResult = _tryDeserializeWith(MAPPER);
69-
// !!! TODO: ought to be DatabindException
70-
assertInstanceOf(JacksonException.class, disabledResult);
69+
assertInstanceOf(DatabindException.class, disabledResult);
7170
// We are throwing exception inside a setter, but InvocationTargetException
7271
// will still be unwrapped
7372
assertInstanceOf(CustomException.class, disabledResult.getCause());

src/test/java/tools/jackson/databind/introspect/TestAutoDetect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public void testVisibilityConfigOverridesForDeser() throws Exception
183183
MAPPER.readValue(JSON, Feature1347DeserBean.class);
184184
fail("Should not pass");
185185
} catch (JacksonException e) { // should probably be something more specific but...
186+
assertInstanceOf(DatabindException.class, e);
186187
verifyException(e, "Should NOT get called");
187188
}
188189

0 commit comments

Comments
 (0)