Skip to content

Commit 54f197b

Browse files
committed
Unwrap JsonMappingException if cause is LengthLimitingJsonProcessingException
1 parent 1a909fc commit 54f197b

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/main/java/com/hubspot/jinjava/objects/serialization/PyishObjectMapper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ private static String getAsPyishString(Object val, boolean forOutput) {
6666
try {
6767
return getAsPyishStringOrThrow(val, forOutput);
6868
} catch (IOException e) {
69-
if (e instanceof LengthLimitingJsonProcessingException) {
69+
IOException unwrapped = e;
70+
if (unwrapped.getCause() instanceof LengthLimitingJsonProcessingException) {
71+
unwrapped = (LengthLimitingJsonProcessingException) unwrapped.getCause();
72+
}
73+
if (unwrapped instanceof LengthLimitingJsonProcessingException) {
7074
throw new OutputTooBigException(
71-
((LengthLimitingJsonProcessingException) e).getMaxSize(),
72-
((LengthLimitingJsonProcessingException) e).getAttemptedSize()
75+
((LengthLimitingJsonProcessingException) unwrapped).getMaxSize(),
76+
((LengthLimitingJsonProcessingException) unwrapped).getAttemptedSize()
7377
);
7478
}
7579
return Objects.toString(val, "");

src/test/java/com/hubspot/jinjava/objects/serialization/PyishObjectMapperTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ public void itLimitsDepth() {
8888
assertThatThrownBy(() -> PyishObjectMapper.getAsPyishStringOrThrow(original))
8989
.as("The string to be serialized is larger than the max output size")
9090
.isInstanceOf(JsonMappingException.class)
91+
.hasCauseInstanceOf(LengthLimitingJsonProcessingException.class)
9192
.hasMessageContaining("Max length of 10000 chars reached");
93+
assertThatThrownBy(() -> PyishObjectMapper.getAsPyishString(original))
94+
.isInstanceOf(OutputTooBigException.class);
9295
} finally {
9396
JinjavaInterpreter.popCurrent();
9497
}

0 commit comments

Comments
 (0)