[CXX-1609] list_collections exception thrown only on cursor iteration Created: 10/Jul/18  Updated: 27/Oct/23  Resolved: 02/Aug/18

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Evgeni Dobranov Assignee: A. Jesse Jiryu Davis
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The database::list_collections() function returns a cursor. The only way to detect an error from this query is to begin iterating on the cursor with cursor::begin(). That is, the following code fragment will go to the catch part: 

auto cursor = db.list_collections(make_document(kvp("$invalidFilter", 1)));
std::cerr << "Cursor creation OK\n";
try {
    auto it = cursor.begin();
} catch (mongocxx::query_exception e) {
    std::cerr << "Query failed. Exception: " << e.what() << "\n";
}

Should this be the way the failed query is detected? It could potentially seem misleading that a cursor was instantiated without a problem, but then using it throws an exception.

One way to alter this is adding an error-related public member to the cursor class for the client to access, but that may complicate the interface a bit.



 Comments   
Comment by A. Jesse Jiryu Davis [ 02/Aug/18 ]

Most other cursor APIs act this way; all should. For consistency, and for your convenience, we'll throw exceptions from one place: cursor iteration.

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