v1::exception and v1::server_error (CXX-3237, CXX-3238) #1501
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves CXX-3237 and CXX-3238 for the
v1::exceptionandv1::server_errorcomponents.As described in #1482 and prior PRs, rather than exposing error categories in the v1 API, the
mongocxx::v1::source_errcerror condition is used to describe the origin of error codes. See the "throw_exception" test case inmongocxx/test/v1/exception.cppfor 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 aMONGOC_ERROR_BSON_INVALIDerror code instead. (Note:bsoncxx::v1exceptions 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_failurefunction 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_categoryerror 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 intomongocxx::v1exceptions. These overloads are comparable tov_noabi::throw_exception<exception_type>()inmongocxx/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, thethrow_exception()function may be extended to select the appropriate write error class to throw according to the fields present in the error document.