Skip to content

Commit

Permalink
[C++] Add an Error Handling section to the Authentication page (#98)
Browse files Browse the repository at this point in the history
* add auth error handling
  • Loading branch information
rachel-mack authored Jan 9, 2025
1 parent 0482339 commit cc2059a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit cc2059a

Please sign in to comment.