Skip to content

Commit 86fb86c

Browse files
committed
Merge branch '2.18'
2 parents 7bcd540 + f90ba05 commit 86fb86c

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/main/java/tools/jackson/core/io/BigDecimalParser.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static BigDecimal parse(final char[] chars, final int off, final int len)
6767
// 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException
6868
// operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both:
6969
} catch (ArithmeticException | NumberFormatException e) {
70-
throw _parseFailure(e, new String(chars, off, len));
70+
throw _parseFailure(e, chars, off, len);
7171
}
7272
}
7373

@@ -122,7 +122,7 @@ public static BigDecimal parseWithFastParser(final char[] ch, final int off, fin
122122
try {
123123
return JavaBigDecimalParser.parseBigDecimal(ch, off, len);
124124
} catch (ArithmeticException | NumberFormatException e) {
125-
throw _parseFailure(e, new String(ch, off, len));
125+
throw _parseFailure(e, ch, off, len);
126126
}
127127
}
128128

@@ -133,18 +133,43 @@ private static NumberFormatException _parseFailure(Exception e, String fullValue
133133
desc = "Not a valid number representation";
134134
}
135135
String valueToReport = _getValueDesc(fullValue);
136-
return new NumberFormatException("Value " + valueToReport
137-
+ " can not be deserialized as `java.math.BigDecimal`, reason: " + desc);
136+
return new NumberFormatException(_generateExceptionMessage(valueToReport, desc));
138137
}
139138

140-
private static String _getValueDesc(String fullValue) {
139+
private static NumberFormatException _parseFailure(final Exception e,
140+
final char[] array,
141+
final int offset,
142+
final int len) {
143+
String desc = e.getMessage();
144+
// 05-Feb-2021, tatu: Alas, JDK mostly has null message so:
145+
if (desc == null) {
146+
desc = "Not a valid number representation";
147+
}
148+
String valueToReport = _getValueDesc(array, offset, len);
149+
return new NumberFormatException(_generateExceptionMessage(valueToReport, desc));
150+
}
151+
152+
private static String _getValueDesc(final String fullValue) {
141153
final int len = fullValue.length();
142154
if (len <= MAX_CHARS_TO_REPORT) {
143155
return String.format("\"%s\"", fullValue);
144156
}
145157
return String.format("\"%s\" (truncated to %d chars (from %d))",
146-
fullValue.substring(0, MAX_CHARS_TO_REPORT),
147-
MAX_CHARS_TO_REPORT, len);
158+
fullValue.substring(0, MAX_CHARS_TO_REPORT),
159+
MAX_CHARS_TO_REPORT, len);
148160
}
149161

162+
private static String _getValueDesc(final char[] array, final int offset, final int len) {
163+
if (len <= MAX_CHARS_TO_REPORT) {
164+
return String.format("\"%s\"", new String(array, offset, len));
165+
}
166+
return String.format("\"%s\" (truncated to %d chars (from %d))",
167+
new String(array, offset, MAX_CHARS_TO_REPORT),
168+
MAX_CHARS_TO_REPORT, len);
169+
}
170+
171+
private static String _generateExceptionMessage(final String valueToReport, final String desc) {
172+
return String.format("Value %s can not be deserialized as `java.math.BigDecimal`, reason: %s" ,
173+
valueToReport, desc);
174+
}
150175
}

0 commit comments

Comments
 (0)