[JAVA-2533] Add type-safe way to detect max document size exceeded Created: 09/Jun/17  Updated: 29/Oct/23  Resolved: 12/Feb/18

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

Type: New Feature Priority: Major - P3
Reporter: Luke Prochazka Assignee: Ross Lawley
Resolution: Fixed Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Case:

 Description   

Add a type-safe way for applications to detect that a BSON document failed to serialize due to the maximum document size supported by MongoDB being exceeded. Possibilities include:

  • adding an enumeration to BsonSerializationException
  • adding a new exception type, e.g. BsonMaximumDocumentSizeExceedException

Original Description

While catching an exception for the error "MaxDocumentSize exceeded...", per this ref, the errorCode is not populated.

It would appear that the BsonSerializationException constructor only accepts strings, while its superclass BSONException can accept both string and a code (among its four constructors).

Looking further at the source for BsonSerializationException, all it does is call super(message) and nothing else.



 Comments   
Comment by Ross Lawley [ 12/Feb/18 ]

3.7.0 will introduce org.bson.BsonMaximumSizeExceededException to cover size exceeded exceptions.

Comment by Githook User [ 12/Feb/18 ]

Author:

{'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley', 'username': 'rozza'}

Message: Added BsonMaximumSizeExceededException

JAVA-2533
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/0aee7384cde74391668af23a908efc74de3dabdd

Comment by Ross Lawley [ 08/Feb/18 ]

PR: https://github.com/rozza/mongo-java-driver/pull/250

Comment by Gabriel Ng [ 19/Jul/17 ]

The previous exception message was not from the latest 3.4.2 java driver. Here is an example from the latest if it helps you find the related code:

org.bson.BsonSerializationException: Document size of 17514051 is larger than maximum of 16777216.

Hope this helps. It would be very nice to have some type of enum to track this error as we have already seen that the exception message changes in different versions of the java driver.

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