Skip to content

Commit c2932be

Browse files
author
Jesse Williamson
authored
CXX-2329 Change type of thrown exception to match documentation (mongodb#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 <[email protected]> * 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 <[email protected]> * Added new mongocxx::error_code for failing to create a resource. Signed-off-by: Jesse Williamson <[email protected]> * Throw mongocxx::logic_error rather than std::logic_error. Signed-off-by: Jesse Williamson <[email protected]> * clang-format Signed-off-by: Jesse Williamson <[email protected]>
1 parent 2f85f9c commit c2932be

File tree

8 files changed

+42
-26
lines changed

8 files changed

+42
-26
lines changed

src/mongocxx/collection.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ class MONGOCXX_API collection {
413413
///
414414
/// @throws mongocxx::query_exception if the count operation fails.
415415
///
416-
/// @note For a fast count of the total documents in a collection, see estimated_document_count().
416+
/// @note For a fast count of the total documents in a collection, see
417+
/// estimated_document_count().
417418
/// @see mongocxx::estimated_document_count
418419
///
419420
std::int64_t count_documents(bsoncxx::document::view_or_value filter,

src/mongocxx/exception/error_code.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 MongoDB Inc.
1+
// Copyright 2015-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -84,6 +84,8 @@ class error_category final : public std::error_category {
8484
return "an invalid client session was provided";
8585
case error_code::k_invalid_transaction_options_object:
8686
return "an invalid transactions options object was provided";
87+
case error_code::k_create_resource_fail:
88+
return "could not create resource";
8789
default:
8890
return "unknown mongocxx error";
8991
}

src/mongocxx/exception/error_code.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 MongoDB Inc.
1+
// Copyright 2015-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -90,6 +90,9 @@ enum class error_code : std::int32_t {
9090
/// A moved-from mongocxx::options::transaction object has been used.
9191
k_invalid_transaction_options_object,
9292

93+
// A resource (server API handle, etc.) could not be created:
94+
k_create_resource_fail,
95+
9396
// Add new constant string message to error_code.cpp as well!
9497
};
9598

src/mongocxx/options/private/server_api.hh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616

17+
#include <mongocxx/exception/logic_error.hpp>
1718
#include <mongocxx/options/apm.hpp>
1819
#include <mongocxx/private/libmongoc.hh>
1920

@@ -33,13 +34,15 @@ static unique_server_api make_server_api(const server_api& opts) {
3334
auto result = libmongoc::server_api_version_from_string(
3435
server_api::version_to_string(opts.get_version()).c_str(), &mongoc_api_version);
3536
if (!result) {
36-
throw std::logic_error{"invalid server API version" +
37-
server_api::version_to_string(opts.get_version())};
37+
throw mongocxx::logic_error{
38+
mongocxx::error_code::k_invalid_parameter,
39+
"invalid server API version" + server_api::version_to_string(opts.get_version())};
3840
}
3941

4042
auto mongoc_server_api_opts = libmongoc::server_api_new(mongoc_api_version);
4143
if (!mongoc_server_api_opts) {
42-
throw std::logic_error{"could not create server API"};
44+
throw mongocxx::logic_error{mongocxx::error_code::k_create_resource_fail,
45+
"could not create server API"};
4346
}
4447

4548
if (opts.strict().value_or(false)) {

src/mongocxx/options/server_api.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <system_error>
16+
1517
#include <bsoncxx/stdx/string_view.hpp>
18+
#include <mongocxx/exception/error_code.hpp>
1619
#include <mongocxx/exception/logic_error.hpp>
1720
#include <mongocxx/options/server_api.hpp>
1821
#include <mongocxx/private/libmongoc.hh>
@@ -29,15 +32,17 @@ std::string server_api::version_to_string(server_api::version version) {
2932
case server_api::version::k_version_1:
3033
return "1";
3134
default:
32-
throw std::logic_error{"invalid server API version"};
35+
throw mongocxx::logic_error{mongocxx::error_code::k_invalid_parameter,
36+
"invalid server API version"};
3337
}
3438
}
3539

3640
server_api::version server_api::version_from_string(stdx::string_view version) {
3741
if (!version.compare("1")) {
3842
return server_api::version::k_version_1;
3943
}
40-
throw std::logic_error{"invalid server API version"};
44+
throw mongocxx::logic_error{mongocxx::error_code::k_invalid_parameter,
45+
"invalid server API version"};
4146
}
4247

4348
server_api::server_api(server_api::version version) : _version(std::move(version)) {}

src/mongocxx/test/collection.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,10 +2333,10 @@ TEST_CASE("read_concern is inherited from parent", "[collection]") {
23332333
}
23342334
}
23352335

2336-
void find_index_and_validate(collection& coll,
2337-
stdx::string_view index_name,
2338-
const std::function<void(bsoncxx::document::view)>& validate =
2339-
[](bsoncxx::document::view) {}) {
2336+
void find_index_and_validate(
2337+
collection& coll,
2338+
stdx::string_view index_name,
2339+
const std::function<void(bsoncxx::document::view)>& validate = [](bsoncxx::document::view) {}) {
23402340
auto cursor = coll.list_indexes();
23412341

23422342
for (auto&& index : cursor) {

src/mongocxx/test/uri.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,20 @@ TEST_CASE("uri::compressors()", "[uri]") {
333333

334334
// Zero is not allowed for heartbeatFrequencyMS.
335335
TEST_CASE("uri::heartbeat_frequency_ms()", "[uri]") {
336-
_test_int32_option("heartbeatFrequencyMS",
337-
[](mongocxx::uri& uri) { return uri.heartbeat_frequency_ms(); },
338-
"1234",
339-
false);
336+
_test_int32_option(
337+
"heartbeatFrequencyMS",
338+
[](mongocxx::uri& uri) { return uri.heartbeat_frequency_ms(); },
339+
"1234",
340+
false);
340341
}
341342

342343
// -1 to 9 are only valid values of zlib compression level.
343344
TEST_CASE("uri::zlib_compression_level()", "[uri]") {
344-
_test_int32_option("zlibCompressionLevel",
345-
[](mongocxx::uri& uri) { return uri.zlib_compression_level(); },
346-
"5",
347-
true);
345+
_test_int32_option(
346+
"zlibCompressionLevel",
347+
[](mongocxx::uri& uri) { return uri.zlib_compression_level(); },
348+
"5",
349+
true);
348350
}
349351

350352
// End special cases.

src/mongocxx/uri.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ std::string uri::password() const {
9797

9898
class read_concern uri::read_concern() const {
9999
auto rc = libmongoc::uri_get_read_concern(_impl->uri_t);
100-
return (class read_concern)(
101-
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc)));
100+
return (class read_concern)(stdx::make_unique<read_concern::impl>(
101+
libmongoc::read_concern_copy(rc)));
102102
}
103103

104104
class read_preference uri::read_preference() const {
105105
auto rp = libmongoc::uri_get_read_prefs_t(_impl->uri_t);
106-
return (class read_preference)(
107-
stdx::make_unique<read_preference::impl>(libmongoc::read_prefs_copy(rp)));
106+
return (class read_preference)(stdx::make_unique<read_preference::impl>(
107+
libmongoc::read_prefs_copy(rp)));
108108
}
109109

110110
std::string uri::replica_set() const {
@@ -129,8 +129,8 @@ std::string uri::username() const {
129129

130130
class write_concern uri::write_concern() const {
131131
auto wc = libmongoc::uri_get_write_concern(_impl->uri_t);
132-
return (class write_concern)(
133-
stdx::make_unique<write_concern::impl>(libmongoc::write_concern_copy(wc)));
132+
return (class write_concern)(stdx::make_unique<write_concern::impl>(
133+
libmongoc::write_concern_copy(wc)));
134134
}
135135

136136
static stdx::optional<stdx::string_view> _string_option(mongoc_uri_t* uri, std::string opt_name) {

0 commit comments

Comments
 (0)