Skip to content

[C++] Add an Error Handling section to the Authentication page #98

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 11 commits into from
Jan 9, 2025
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
14 changes: 13 additions & 1 deletion source/includes/authentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ auto client = mongocxx::client(uri);
auto uri = mongocxx::uri("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"authMechanism=PLAIN&tls=true");
auto client = mongocxx::client(uri);
// end-plain
// end-plain

// start-auth-err
try {
auto uri = mongocxx::uri("<connection string>");
auto client = mongocxx::client(uri);

client["db"].run_command(<any command requiring authorization>);

} catch (const mongocxx::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
// end-auth-err
17 changes: 17 additions & 0 deletions source/security/authentication.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ URI to ``"MONGODB-AWS"``, as shown in the following example:
to retrieve credentials only from an EC2 instance, ensure that the
environment variables are not set.

Error Handling
--------------

You can handle authentication errors by catching ``mongocxx::exception``
errors. Inspect the error message string by using the ``what()`` method, as
shown in the following example:

.. literalinclude:: /includes/authentication.cpp
:language: cpp
:copyable: true
:start-after: // start-auth-err
:end-before: // end-auth-err

For more information, see `Operation Exceptions
<{+api+}/topic-mongocxx-examples-operation-exceptions.html>`__ in the API
documentation.

API Documentation
-----------------

Expand Down
Loading