Skip to content

Commit

Permalink
Fix #1007: add more information on StreamReadConstraints violation …
Browse files Browse the repository at this point in the history
…exception message (#1008)
  • Loading branch information
cowtowncoder authored May 1, 2023
1 parent 7d03ce2 commit 336a402
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ JSON library.

2.16.0 (not yet released)

No changes since 2.15
#1007: Improve error message for `StreamReadConstraints` violations

2.15.1 (not yet released)

Expand Down
46 changes: 35 additions & 11 deletions src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ public int getMaxStringLength() {
public void validateNestingDepth(int depth) throws StreamConstraintsException
{
if (depth > _maxNestingDepth) {
throw new StreamConstraintsException(String.format("Depth (%d) exceeds the maximum allowed nesting depth (%d)",
depth, _maxNestingDepth));
throw _constructException(
"Document nesting depth (%d) exceeds the maximum allowed (%d, from %s)",
depth, _maxNestingDepth,
_constrainRef("getMaxNestingDepth"));
}
}

Expand All @@ -249,8 +251,10 @@ public void validateNestingDepth(int depth) throws StreamConstraintsException
public void validateFPLength(int length) throws StreamConstraintsException
{
if (length > _maxNumLen) {
throw new StreamConstraintsException(String.format("Number length (%d) exceeds the maximum length (%d)",
length, _maxNumLen));
throw _constructException(
"Number value length (%d) exceeds the maximum allowed (%d, from %s)",
length, _maxNumLen,
_constrainRef("getMaxNumberLength"));
}
}

Expand All @@ -268,8 +272,10 @@ public void validateFPLength(int length) throws StreamConstraintsException
public void validateIntegerLength(int length) throws StreamConstraintsException
{
if (length > _maxNumLen) {
throw new StreamConstraintsException(String.format("Number length (%d) exceeds the maximum length (%d)",
length, _maxNumLen));
throw _constructException(
"Number value length (%d) exceeds the maximum allowed (%d, from %s)",
length, _maxNumLen,
_constrainRef("getMaxNumberLength"));
}
}

Expand All @@ -287,8 +293,10 @@ public void validateIntegerLength(int length) throws StreamConstraintsException
public void validateStringLength(int length) throws StreamConstraintsException
{
if (length > _maxStringLen) {
throw new StreamConstraintsException(String.format("String length (%d) exceeds the maximum length (%d)",
length, _maxStringLen));
throw _constructException(
"String value length (%d) exceeds the maximum allowed (%d, from %s)",
length, _maxStringLen,
_constrainRef("getMaxStringLength"));
}
}

Expand All @@ -315,9 +323,25 @@ public void validateBigIntegerScale(int scale) throws StreamConstraintsException
final int limit = MAX_BIGINT_SCALE_MAGNITUDE;

if (absScale > limit) {
throw new StreamConstraintsException(String.format(
"BigDecimal scale (%d) magnitude exceeds maximum allowed (%d)",
scale, limit));
throw _constructException(
"BigDecimal scale (%d) magnitude exceeds the maximum allowed (%d)",
scale, limit);
}
}

/*
/**********************************************************************
/* Error reporting
/**********************************************************************
*/

// @since 2.16
protected StreamConstraintsException _constructException(String msgTemplate, Object... args) throws StreamConstraintsException {
throw new StreamConstraintsException(String.format(msgTemplate, args));
}

// @since 2.16
protected String _constrainRef(String method) {
return "`StreamReadConstraints."+method+"()`";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private void _testDeepNesting(JsonParser p) throws Exception
while (p.nextToken() != null) { }
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertEquals("Depth ("+(MAX_NESTING+1)
+") exceeds the maximum allowed nesting depth ("+MAX_NESTING+")", e.getMessage());
assertEquals("Document nesting depth (1001) exceeds the maximum allowed (1000, from `StreamReadConstraints.getMaxNestingDepth()`)",
e.getMessage());
}
}

Expand All @@ -58,8 +58,8 @@ private void _testLegacyConstraintSettingTest(JsonParser p, int maxNesting) thro
while (p.nextToken() != null) { }
fail("expected StreamConstraintsException");
} catch (StreamConstraintsException e) {
assertEquals("Depth ("+(maxNesting+1)
+") exceeds the maximum allowed nesting depth ("+maxNesting+")", e.getMessage());
assertEquals("Document nesting depth (41) exceeds the maximum allowed (40, from `StreamReadConstraints.getMaxNestingDepth()`)",
e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public void testBigBigDecimalsBytes() throws Exception
_testBigBigDecimals(MODE_INPUT_STREAM, false);
fail("Should not pass");
} catch (StreamConstraintsException e) {
verifyException(e, "Invalid numeric value ", "exceeds the maximum length");
verifyException(e, "Invalid numeric value ", "exceeds the maximum");
}
try {
_testBigBigDecimals(MODE_INPUT_STREAM_THROTTLED, false);
fail("Should not pass");
} catch (StreamConstraintsException jpe) {
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum length");
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum");
}
}

Expand All @@ -50,7 +50,7 @@ public void testBigBigDecimalsCharsFailByDefault() throws Exception
_testBigBigDecimals(MODE_READER, false);
fail("Should not pass");
} catch (StreamConstraintsException jpe) {
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum length");
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum");
}
}

Expand All @@ -65,7 +65,7 @@ public void testBigBigDecimalsDataInputFailByDefault() throws Exception
_testBigBigDecimals(MODE_DATA_INPUT, false);
fail("Should not pass");
} catch (StreamConstraintsException jpe) {
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum length");
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum allowed");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void bigIntegerViaBigDecimal() throws Exception {
p.getBigIntegerValue();
Assert.fail("Should not pass");
} catch (StreamConstraintsException e) {
Assert.assertEquals("BigDecimal scale (-25000000) magnitude exceeds maximum allowed (100000)", e.getMessage());
Assert.assertEquals("BigDecimal scale (-25000000) magnitude exceeds the maximum allowed (100000)", e.getMessage());
}
}
}
Expand All @@ -37,7 +37,7 @@ public void tinyIntegerViaBigDecimal() throws Exception {
p.getBigIntegerValue();
Assert.fail("Should not pass");
} catch (StreamConstraintsException e) {
Assert.assertEquals("BigDecimal scale (25000000) magnitude exceeds maximum allowed (100000)", e.getMessage());
Assert.assertEquals("BigDecimal scale (25000000) magnitude exceeds the maximum allowed (100000)", e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public void testLongAsciiStringsSmallLimit() throws IOException
fail("expected IOException");
} catch (IOException ie) {
assertTrue("unexpected exception message: " + ie.getMessage(),
ie.getMessage().startsWith("String length"));
ie.getMessage().startsWith("String value length"));
assertTrue("unexpected exception message: " + ie.getMessage(),
ie.getMessage().endsWith("exceeds the maximum length (100)"));
ie.getMessage().contains("exceeds the maximum allowed (100"));
}
}

Expand Down

0 comments on commit 336a402

Please sign in to comment.