Skip to content

Conversation

@eramongodb
Copy link
Contributor

Resolves CXX-3237 and CXX-3238 for the v1::exception and v1::server_error components.

As described in #1482 and prior PRs, rather than exposing error categories in the v1 API, the mongocxx::v1::source_errc error condition is used to describe the origin of error codes. See the "throw_exception" test case in mongocxx/test/v1/exception.cpp for reference. On the off-chance that a bson library error code is returned by the mongoc API, its code is stored in the error message ("bson error code <code>: <message>") and converted into a MONGOC_ERROR_BSON_INVALID error code instead. (Note: bsoncxx::v1 exceptions are not converted.)

There is the possibility that the mongoc API returns a SASL library error code. This is probably both unlikely and not intentional given how the _mongoc_cyrus_is_failure function is implemented (mongoc should do its best to convert the SASL error code into a mongoc error code). Nevertheless, if it does manage to be returned by the mongoc API, it is converted into a (undocumented) sasl_error_category error code for diagnosis. When an error category has a dedicated error code type, std::error_code::message(v) formats the error code message using that type, e.g. "mongoc_error_code_t:<code>". Otherwise, the message is formatted as "<source> error code <code>", e.g. "server error code 12345".

The v1::exception::internal::throw_exception() overloads are the primary method by which most errors returned by mongoc will be converted into mongocxx::v1 exceptions. These overloads are comparable to v_noabi::throw_exception<exception_type>() in mongocxx/mongoc_error.hh; unlike the v_noabi overloads, the v1 overloads determine the exception class to throw according to the provided arguments. This is the primary means by which CXX-834 will be addressed going forward, e.g. if we decide to implement the write-related error classes from the CRUD specification, the throw_exception() function may be extended to select the appropriate write error class to throw according to the fields present in the error document.

@eramongodb eramongodb requested a review from kevinAlbs November 14, 2025 20:04
@eramongodb eramongodb self-assigned this Nov 14, 2025
@eramongodb eramongodb requested a review from a team as a code owner November 14, 2025 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant