Skip to content

Commit cc2059a

Browse files
authored
[C++] Add an Error Handling section to the Authentication page (#98)
* add auth error handling
1 parent 0482339 commit cc2059a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

source/includes/authentication.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ auto client = mongocxx::client(uri);
4545
auto uri = mongocxx::uri("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
4646
"authMechanism=PLAIN&tls=true");
4747
auto client = mongocxx::client(uri);
48-
// end-plain
48+
// end-plain
49+
50+
// start-auth-err
51+
try {
52+
auto uri = mongocxx::uri("<connection string>");
53+
auto client = mongocxx::client(uri);
54+
55+
client["db"].run_command(<any command requiring authorization>);
56+
57+
} catch (const mongocxx::exception& ex) {
58+
std::cerr << "Error: " << ex.what() << std::endl;
59+
}
60+
// end-auth-err

source/security/authentication.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ URI to ``"MONGODB-AWS"``, as shown in the following example:
273273
to retrieve credentials only from an EC2 instance, ensure that the
274274
environment variables are not set.
275275

276+
Error Handling
277+
--------------
278+
279+
You can handle authentication errors by catching ``mongocxx::exception``
280+
errors. Inspect the error message string by using the ``what()`` method, as
281+
shown in the following example:
282+
283+
.. literalinclude:: /includes/authentication.cpp
284+
:language: cpp
285+
:copyable: true
286+
:start-after: // start-auth-err
287+
:end-before: // end-auth-err
288+
289+
For more information, see `Operation Exceptions
290+
<{+api+}/topic-mongocxx-examples-operation-exceptions.html>`__ in the API
291+
documentation.
292+
276293
API Documentation
277294
-----------------
278295

0 commit comments

Comments
 (0)