Skip to content

Commit 35f3a33

Browse files
committed
add auth error handling
1 parent 0482339 commit 35f3a33

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

source/includes/authentication.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,14 @@ 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("mongodb://<>"
53+
"authMechanism=<>");
54+
auto client = mongocxx::client(uri);
55+
} catch (const mongocxx::exception& e) {
56+
std::cerr << "Error: " << e.code() << std::endl;
57+
}
58+
// end-auth-err

source/security/authentication.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ 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+
Authentication errors are thrown as ``mongocxx::exception``` errors. You can find
280+
the error code by using the ``code()`` method.
281+
282+
.. literalinclude:: /includes/authentication.cpp
283+
:language: cpp
284+
:copyable: true
285+
:start-after: // start-auth-err
286+
:end-before: // end-auth-err
287+
276288
API Documentation
277289
-----------------
278290

0 commit comments

Comments
 (0)