[CXX-2329] options::server_api::version_from_string() throws the wrong type Created: 27/Jul/21  Updated: 28/Oct/23  Resolved: 04/Aug/21

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 3.7.0, 3.7.0-beta1

Type: Bug Priority: Unknown
Reporter: Jesse Williamson (Inactive) Assignee: Jesse Williamson (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CXX-2138 Versioned MongoDB API for Drivers Closed
Backwards Compatibility: Minor Change

 Description   

The doxygen documentation for version_from_string() claims to throw a mongocxx::logic_error on invalid argument, but instead throws a std::logic_error.

 

Note: as part of this change, I recommend investigating version_to_string() as well.



 Comments   
Comment by Jesse Williamson (Inactive) [ 04/Aug/21 ]

Users catching std::logic_error (what was actually thrown by the code, as opposed to what was documented) will need to update their code to catch mongocxx::logic_error (and check the error code to distinguish invalid_parameter from some other error).

Comment by Githook User [ 30/Jul/21 ]

Author:

{'name': 'Jesse Williamson', 'email': 'jesse.williamson@mongodb.com', 'username': 'chardan'}

Message: CXX-2329 Change type of thrown exception to match documentation (#809)

  • Fix CXX-2329; change type of thrown exception to match documentation.

Modify exceptions throw to use mongocxx::logic_error.

Adds std::error_code/std::errc plumbing, as mongocxx::exception
derives from std::system_exception, and mongocxx::logic_error
derives in turn from that.

See: https://jira.mongodb.org/browse/CXX-2329

Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>

  • Updated thrown type to use mongocxx error codes.

We provide our own error codes, and there's precedent for using
k_invalid_argument.

Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>

  • Added new mongocxx::error_code for failing to create a resource.

Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>

  • Throw mongocxx::logic_error rather than std::logic_error.

Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>

  • clang-format

Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/c2932becd7b69dfa15bfc6a8cda6038dfc33a600

Comment by Jesse Williamson (Inactive) [ 27/Jul/21 ]

Sure enough, in options/server_api.cpp we see:

std::string server_api::version_to_string(server_api::version version) { 
    switch (version) {        
        case server_api::version::k_version_1:            
                        return "1";        
        default:             
                        throw std::logic_error\\{"invalid server API version"}; 
    } 
 }
 
server_api::version server_api::version_from_string(stdx::string_view version) { 
    if (!version.compare("1"))  {        
        return server_api::version::k_version_1;    
    }
 
    throw std::logic_error\{"invalid server API version"}; 
 }

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