From 1aebcb3ad6c2d6a5175711548f1d127ee376ae43 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 15 Dec 2023 16:56:51 -0800 Subject: [PATCH] Further improvements to error handling wrt Smile, number coercion --- .../dataformat/smile/SmileParserBase.java | 18 +++++++++--------- .../smile/parse/NumberParsingTest.java | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParserBase.java b/smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParserBase.java index ac1b61b0d..6a3000b5f 100644 --- a/smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParserBase.java +++ b/smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParserBase.java @@ -589,30 +589,30 @@ protected final void convertNumberToInt() throws IOException // Let's verify it's lossless conversion by simple roundtrip int result = (int) _numberLong; if (((long) result) != _numberLong) { - _reportError("Numeric value (%s) out of range of int", getText()); + reportOverflowInt(String.valueOf(_numberLong)); } _numberInt = result; } else if ((_numTypesValid & NR_BIGINT) != 0) { if (BI_MIN_INT.compareTo(_numberBigInt) > 0 || BI_MAX_INT.compareTo(_numberBigInt) < 0) { - reportOverflowInt(); + reportOverflowInt(String.valueOf(_numberBigInt)); } _numberInt = _numberBigInt.intValue(); } else if ((_numTypesValid & NR_DOUBLE) != 0) { // Need to check boundaries if (_numberDouble < MIN_INT_D || _numberDouble > MAX_INT_D) { - reportOverflowInt(); + reportOverflowInt(String.valueOf(_numberDouble)); } _numberInt = (int) _numberDouble; } else if ((_numTypesValid & NR_FLOAT) != 0) { if (_numberFloat < MIN_INT_D || _numberFloat > MAX_INT_D) { - reportOverflowInt(); + reportOverflowInt(String.valueOf(_numberFloat)); } _numberInt = (int) _numberFloat; } else if ((_numTypesValid & NR_BIGDECIMAL) != 0) { if (BD_MIN_INT.compareTo(_numberBigDecimal) > 0 || BD_MAX_INT.compareTo(_numberBigDecimal) < 0) { - reportOverflowInt(); + reportOverflowInt(String.valueOf(_numberBigDecimal)); } _numberInt = _numberBigDecimal.intValue(); } else { @@ -629,23 +629,23 @@ protected final void convertNumberToLong() throws IOException } else if ((v & NR_BIGINT) != 0) { if (BI_MIN_LONG.compareTo(_numberBigInt) > 0 || BI_MAX_LONG.compareTo(_numberBigInt) < 0) { - reportOverflowLong(); + reportOverflowLong(String.valueOf(_numberBigInt)); } _numberLong = _numberBigInt.longValue(); } else if ((v & NR_DOUBLE) != 0) { if (_numberDouble < MIN_LONG_D || _numberDouble > MAX_LONG_D) { - reportOverflowLong(); + reportOverflowLong(String.valueOf(_numberDouble)); } _numberLong = (long) _numberDouble; } else if ((v & NR_FLOAT) != 0) { if (_numberFloat < MIN_LONG_D || _numberFloat > MAX_LONG_D) { - reportOverflowInt(); + reportOverflowLong(String.valueOf(_numberFloat)); } _numberLong = (long) _numberFloat; } else if ((v & NR_BIGDECIMAL) != 0) { if (BD_MIN_LONG.compareTo(_numberBigDecimal) > 0 || BD_MAX_LONG.compareTo(_numberBigDecimal) < 0) { - reportOverflowLong(); + reportOverflowLong(String.valueOf(_numberBigDecimal)); } _numberLong = _numberBigDecimal.longValue(); } else { diff --git a/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/NumberParsingTest.java b/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/NumberParsingTest.java index d72ad202f..c8287bde5 100644 --- a/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/NumberParsingTest.java +++ b/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/NumberParsingTest.java @@ -5,6 +5,7 @@ import java.math.BigInteger; import com.fasterxml.jackson.core.*; +import com.fasterxml.jackson.core.exc.InputCoercionException; import com.fasterxml.jackson.core.exc.StreamConstraintsException; import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile; import com.fasterxml.jackson.dataformat.smile.SmileFactory; @@ -485,7 +486,7 @@ public void testMixedAccessForInts() throws IOException try { p.getIntValue(); fail("Should not pass"); - } catch (JsonParseException e) { + } catch (InputCoercionException e) { verifyException(e, "Numeric value"); verifyException(e, "out of range of int"); }