[JAVA-2937] Decimal128 throws AssertionError instead of NumberFormatException Created: 22/Aug/18  Updated: 27/Oct/23  Resolved: 01/Oct/18

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andrei Sereda Assignee: Jeffrey Yemin
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by JAVA-2982 Decimal128 is throwing AssertionError... Closed
Related

 Description   

According to documentation NumberFormatException should be thrown when BigDecimal is out of range for Decimal128 but looking at the code AssertionError is being used instead :

if ((exponent < MIN_EXPONENT) || (exponent > MAX_EXPONENT)) {
throw new AssertionError("Exponent is out of range for Decimal128 encoding: " + exponent); 
}



 Comments   
Comment by Andrei Sereda [ 29/Sep/18 ]

Ross,

I have tried to simulate assertion error manually but unfortunately couldn't. All the checks insideĀ clampAndRound / ensureExactRounding methods are throwing NumberFormatException (which is expected behaviour).

If I observe AssertionError will post the exact value here.

Comment by Ross Lawley [ 19/Sep/18 ]

asereda could you provide the engineering string representation of the BigDecimal that triggers the Assertion error?

Comment by Andrei Sereda [ 22/Aug/18 ]

By inspection (and tests).

We have custom Codec for BigDecimal which fallsback to String if Decimal128 can't be used :

      final BigDecimal decimal = ....;
      try {
        return writer.writeDecimal128(new Decimal128(decimal));
      } catch (NumberFormatException ex) {
        // fallback to serializing to string
        return writer.writeString(decimal.toPlainString());
      }

Comment by Jeffrey Yemin [ 22/Aug/18 ]

Hi asereda

Thanks for the report. Can you let us know how this is impacting you? Is it something you encountered while running an application, or did you find it by inspection?

Generated at Thu Feb 08 08:58:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.