Skip to content

Commit 1454816

Browse files
committed
Fix #1007: add more information on StreamReadConstraints violation exception msg
1 parent 7d03ce2 commit 1454816

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ public int getMaxStringLength() {
224224
public void validateNestingDepth(int depth) throws StreamConstraintsException
225225
{
226226
if (depth > _maxNestingDepth) {
227-
throw new StreamConstraintsException(String.format("Depth (%d) exceeds the maximum allowed nesting depth (%d)",
228-
depth, _maxNestingDepth));
227+
throw _constructException(
228+
"Document nesting depth (%d) exceeds the maximum allowed (%d, from %s)",
229+
depth, _maxNestingDepth,
230+
_constrainRef("getMaxNestingDepth"));
229231
}
230232
}
231233

@@ -249,8 +251,10 @@ public void validateNestingDepth(int depth) throws StreamConstraintsException
249251
public void validateFPLength(int length) throws StreamConstraintsException
250252
{
251253
if (length > _maxNumLen) {
252-
throw new StreamConstraintsException(String.format("Number length (%d) exceeds the maximum length (%d)",
253-
length, _maxNumLen));
254+
throw _constructException(
255+
"Number value length (%d) exceeds the maximum allowed (%d, from %s)",
256+
length, _maxNumLen,
257+
_constrainRef("getMaxNumberLength"));
254258
}
255259
}
256260

@@ -268,8 +272,10 @@ public void validateFPLength(int length) throws StreamConstraintsException
268272
public void validateIntegerLength(int length) throws StreamConstraintsException
269273
{
270274
if (length > _maxNumLen) {
271-
throw new StreamConstraintsException(String.format("Number length (%d) exceeds the maximum length (%d)",
272-
length, _maxNumLen));
275+
throw _constructException(
276+
"Number value length (%d) exceeds the maximum allowed (%d, from %s)",
277+
length, _maxNumLen,
278+
_constrainRef("getMaxNumberLength"));
273279
}
274280
}
275281

@@ -287,8 +293,10 @@ public void validateIntegerLength(int length) throws StreamConstraintsException
287293
public void validateStringLength(int length) throws StreamConstraintsException
288294
{
289295
if (length > _maxStringLen) {
290-
throw new StreamConstraintsException(String.format("String length (%d) exceeds the maximum length (%d)",
291-
length, _maxStringLen));
296+
throw _constructException(
297+
"String value length (%d) exceeds the maximum allowed (%d, from %s)",
298+
length, _maxStringLen,
299+
_constrainRef("getMaxStringLength"));
292300
}
293301
}
294302

@@ -315,9 +323,23 @@ public void validateBigIntegerScale(int scale) throws StreamConstraintsException
315323
final int limit = MAX_BIGINT_SCALE_MAGNITUDE;
316324

317325
if (absScale > limit) {
318-
throw new StreamConstraintsException(String.format(
319-
"BigDecimal scale (%d) magnitude exceeds maximum allowed (%d)",
320-
scale, limit));
326+
throw _constructException(
327+
"BigDecimal scale (%d) magnitude exceeds the maximum allowed (%d)",
328+
scale, limit);
321329
}
322330
}
331+
332+
/*
333+
/**********************************************************************
334+
/* Error reporting
335+
/**********************************************************************
336+
*/
337+
338+
protected StreamConstraintsException _constructException(String msgTemplate, Object... args) throws StreamConstraintsException {
339+
throw new StreamConstraintsException(String.format(msgTemplate, args));
340+
}
341+
342+
protected String _constrainRef(String method) {
343+
return "`StreamReadConstraints."+method+"()`";
344+
}
323345
}

src/test/java/com/fasterxml/jackson/core/constraints/DeeplyNestedContentReadTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ private void _testDeepNesting(JsonParser p) throws Exception
3232
while (p.nextToken() != null) { }
3333
fail("expected StreamConstraintsException");
3434
} catch (StreamConstraintsException e) {
35-
assertEquals("Depth ("+(MAX_NESTING+1)
36-
+") exceeds the maximum allowed nesting depth ("+MAX_NESTING+")", e.getMessage());
35+
assertEquals("Document nesting depth (1001) exceeds the maximum allowed (1000, from `StreamReadConstraints.getMaxNestingDepth()`)",
36+
e.getMessage());
3737
}
3838
}
3939

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

src/test/java/com/fasterxml/jackson/core/constraints/LargeNumberReadTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public void testBigBigDecimalsBytes() throws Exception
3434
_testBigBigDecimals(MODE_INPUT_STREAM, false);
3535
fail("Should not pass");
3636
} catch (StreamConstraintsException e) {
37-
verifyException(e, "Invalid numeric value ", "exceeds the maximum length");
37+
verifyException(e, "Invalid numeric value ", "exceeds the maximum");
3838
}
3939
try {
4040
_testBigBigDecimals(MODE_INPUT_STREAM_THROTTLED, false);
4141
fail("Should not pass");
4242
} catch (StreamConstraintsException jpe) {
43-
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum length");
43+
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum");
4444
}
4545
}
4646

@@ -50,7 +50,7 @@ public void testBigBigDecimalsCharsFailByDefault() throws Exception
5050
_testBigBigDecimals(MODE_READER, false);
5151
fail("Should not pass");
5252
} catch (StreamConstraintsException jpe) {
53-
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum length");
53+
verifyException(jpe, "Invalid numeric value ", "exceeds the maximum");
5454
}
5555
}
5656

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

0 commit comments

Comments
 (0)