CXX-1110 add CHECK_THROWS_WITH_CODE #1300
Merged
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-1110. Verified by this patch (containing the proposed changes in #1298).
Precursor changes to faciliate the improvement and addition of tests for bsoncxx and mongocxx error codes and error categories as part of CXX-834 and CXX-2745.
The two new macros
(CHECK|REQUIRE)_THROWS_WITH_CODE
mirror the existing Catch2 exception assertion macros(CHECK|REQUIRE)_THROWS_WITH
. It is expected to be used as follows:The exception type must be or derive from
std::system_error
which provides the error code (via.code()
) to be compared against. Otherwise, the test assertion fails via an always-false*_THROWS_WITH(throw, std::system_error)
to reuse Catch's exception type-match error messages.The option to define and use a custom Catch matcher for use with
*_THROWS_WITH
was rejected due to not correctly supporting Catch test exception propagation and not fully supporting customization of the resulting error messages due to unavoidable internal exception handler routines being executed within Catch. It was also comparatively unwieldy to write due to having to construct the custom matcher as an argument (its omission would just lead to the same*_THROWS_WITH_CODE
interface as proposed by this PR):CHECK_THROWS_WITH(throwing-expr, Code(error-code));
Although Catch seems to support string-making a
std::error_code
(e.g.std::errc::invalid_argument
->generic:22
), it doesn't seem to supportstd::error_condition
, so a specialization is also added which uses the same format as error codes. This produces assertion messages like the following:and in the event of an unexpected exception whose type does not provide
ex.code() -> std::error_code
: