Skip to content

CXX-1643 Replace mongoc_uri_new for better exception message #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mongocxx/private/libmongoc_symbols.hh
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(uri_get_tls)
MONGOCXX_LIBMONGOC_SYMBOL(uri_get_string)
MONGOCXX_LIBMONGOC_SYMBOL(uri_get_username)
MONGOCXX_LIBMONGOC_SYMBOL(uri_get_write_concern)
MONGOCXX_LIBMONGOC_SYMBOL(uri_new)
MONGOCXX_LIBMONGOC_SYMBOL(uri_new_with_error)
MONGOCXX_LIBMONGOC_SYMBOL(write_concern_copy)
MONGOCXX_LIBMONGOC_SYMBOL(write_concern_destroy)
MONGOCXX_LIBMONGOC_SYMBOL(write_concern_get_journal)
Expand Down
5 changes: 5 additions & 0 deletions src/mongocxx/test/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ TEST_CASE("URI", "[uri]") {
mongocxx::uri{invalid};
} catch (const mongocxx::logic_error& e) {
REQUIRE(e.code() == mongocxx::error_code::k_invalid_uri);

std::string invalid_schema =
"Invalid URI Schema, expecting 'mongodb://' or 'mongodb+srv://': ";

REQUIRE(e.what() == invalid_schema + e.code().message());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this test is necessary but I added it in the moment to make sure we were successfully getting the correct error message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This works. There's also a REQUIRE_THROWS_WITH that takes a regex so you can assert things like "the error contains the words invalid URI" case-insensitively. But no need to change this.

}
}

Expand Down
10 changes: 7 additions & 3 deletions src/mongocxx/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ uri::uri(std::unique_ptr<impl>&& implementation) {
_impl.reset(implementation.release());
}

uri::uri(bsoncxx::string::view_or_value uri_string)
: _impl(stdx::make_unique<impl>(libmongoc::uri_new(uri_string.terminated().data()))) {
uri::uri(bsoncxx::string::view_or_value uri_string) {
bson_error_t error;

_impl = stdx::make_unique<impl>(
libmongoc::uri_new_with_error(uri_string.terminated().data(), &error));

if (_impl->uri_t == nullptr) {
throw logic_error{error_code::k_invalid_uri};
throw logic_error{error_code::k_invalid_uri, error.message};
}
}

Expand Down