[CXX-862] The element and array type operator[] function should not throw Created: 10/Mar/16  Updated: 08/Jan/24  Resolved: 22/Mar/16

Status: Closed
Project: C++ Driver
Component/s: API
Affects Version/s: 3.0.0
Fix Version/s: 3.0.1

Type: Improvement Priority: Major - P3
Reporter: Andrew Morrow (Inactive) Assignee: Andrew Morrow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

There is an awkward inconsistency in the operator[] interface for bsoncxx::document::element and bsoncxx::document::array.

If element::operator[] or array::operator[] is called and a matching element is not found, we return an invalid element.

Similarly, if you call element::operator[] (either overload) and a matching element is not found, we also return an invalid element, as long as the type of the current element is document or array.

However, if you call element::operator[] (either overload) on something that is neither a document nor an array, an exception is thrown.

This means that a statement like:

foo[a][b][c][d]

May either throw or return an invalid element, depending on the shape of the document foo.

This is pretty hard to handle. We should make it either throw, always, on a missing element, or degrade to an invalid element, always.



 Comments   
Comment by Githook User [ 22/Mar/16 ]

Author:

{u'username': u'acmorrow', u'name': u'Andrew Morrow', u'email': u'acm@mongodb.com'}

Message: CXX-862 Fixup formatting
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/47608de0d7507784fca50f9d57c78b24be29bd88

Comment by Githook User [ 22/Mar/16 ]

Author:

{u'username': u'acmorrow', u'name': u'Andrew Morrow', u'email': u'acm@mongodb.com'}

Message: CXX-862 Fixup example after changes
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/229e91fd37928da9c00d648b047e62b507489dae

Comment by Githook User [ 22/Mar/16 ]

Author:

{u'username': u'acmorrow', u'name': u'Andrew Morrow', u'email': u'acm@mongodb.com'}

Message: CXX-862 Don't throw on invalid element access by index or key
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/9738aef1169b03cdbb54fd0fe789adce4a1df1df

Comment by Andrew Morrow (Inactive) [ 21/Mar/16 ]

https://github.com/mongodb/mongo-cxx-driver/pull/471

Generated at Wed Feb 07 22:00:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.