|
| 1 | +// Copyright 2015 MongoDB Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <cstdlib> |
| 16 | +#include <iostream> |
| 17 | + |
| 18 | +#include <bsoncxx/builder/stream/document.hpp> |
| 19 | +#include <bsoncxx/json.hpp> |
| 20 | +#include <mongocxx/client.hpp> |
| 21 | +#include <mongocxx/exception/bulk_write_exception.hpp> |
| 22 | +#include <mongocxx/exception/error_code.hpp> |
| 23 | +#include <mongocxx/exception/server_error_code.hpp> |
| 24 | +#include <mongocxx/exception/logic_error.hpp> |
| 25 | +#include <mongocxx/exception/operation_exception.hpp> |
| 26 | +#include <mongocxx/instance.hpp> |
| 27 | +#include <mongocxx/uri.hpp> |
| 28 | + |
| 29 | +using bsoncxx::builder::stream::close_document; |
| 30 | +using bsoncxx::builder::stream::document; |
| 31 | +using bsoncxx::builder::stream::finalize; |
| 32 | +using bsoncxx::builder::stream::open_document; |
| 33 | + |
| 34 | +int main(int, char**) { |
| 35 | + mongocxx::instance inst{}; |
| 36 | + mongocxx::client conn{mongocxx::uri{}}; |
| 37 | + |
| 38 | + // @begin: cpp-logic-error |
| 39 | + // Using an uninitialized collection throws a mongocxx::logic_error. |
| 40 | + // A mongocxx::logic_error is-a mongocxx::exception is-a std::system_error. |
| 41 | + mongocxx::collection coll; |
| 42 | + try { |
| 43 | + coll.name(); |
| 44 | + } catch (const mongocxx::logic_error& e) { |
| 45 | + std::cout << "Using an uninitialized collection throws:" << std::endl; |
| 46 | + |
| 47 | + // Local errors, like those generated by mis-use of the |
| 48 | + // library itself, exist in the mongocxx 'error_category' |
| 49 | + // object. This is to be distinguished from the mongocxx |
| 50 | + // 'server_error_category' object, which tracks errors |
| 51 | + // returned by the server. |
| 52 | + // |
| 53 | + // NOTE: At this time, the server_error_category has no |
| 54 | + // associated symbolic errors. You must use numeric codes to |
| 55 | + // understand the errors returned in this category. See below |
| 56 | + // for more discussion. |
| 57 | + if (e.code().category() != mongocxx::error_category()) { |
| 58 | + return EXIT_FAILURE; |
| 59 | + } |
| 60 | + |
| 61 | + // We can compare the error_code to a known mongocxx::error_code. |
| 62 | + if (e.code() != mongocxx::error_code::k_invalid_collection_object) { |
| 63 | + return EXIT_FAILURE; |
| 64 | + } |
| 65 | + |
| 66 | + std::cout << e.what() << std::endl << std::endl; |
| 67 | + } |
| 68 | + // @end: cpp-logic-error |
| 69 | + |
| 70 | + // @begin: cpp-operation-exception |
| 71 | + // Renaming a collection that does not exist throws a mongocxx::operation_exception. |
| 72 | + // A mongocxx::operation_exception is-a mongocxx::exception is-a std::system_error. |
| 73 | + coll = conn["test"]["coll"]; |
| 74 | + coll.drop(); |
| 75 | + try { |
| 76 | + coll.rename("coll2"); |
| 77 | + } catch (const mongocxx::operation_exception& e) { |
| 78 | + std::cout << "Renaming a collection that does not exist throws:" << std::endl; |
| 79 | + |
| 80 | + // We expect this error to arise from the server |
| 81 | + if (e.code().category() != mongocxx::server_error_category()) { |
| 82 | + return EXIT_FAILURE; |
| 83 | + } |
| 84 | + |
| 85 | + // We can compare the error_code to a known server side error |
| 86 | + // code number. Please see |
| 87 | + // https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err |
| 88 | + // for details on the sort of error codes the server might |
| 89 | + // return. In this case, 26 means 'NamespaceNotFound'. |
| 90 | + // |
| 91 | + // NOTE: Due to a design flaw in the C driver which currently |
| 92 | + // is the underlying implementation of the C++11 driver, it is |
| 93 | + // not possible to reliably distinguish between error codes |
| 94 | + // generated locally by libmongoc, or error codes generated |
| 95 | + // remotely by the server. Clients of libmongocxx are |
| 96 | + // therefore required to interpret the code numerically and |
| 97 | + // contextually. When the C driver fixes its error handling |
| 98 | + // strategy, we will add symbolic codes for server errors, and |
| 99 | + // report local library errors from libmongoc in the |
| 100 | + // mongocxx::error_code enumeration. |
| 101 | + if (e.code().value() != 26) { |
| 102 | + return EXIT_FAILURE; |
| 103 | + } |
| 104 | + |
| 105 | + std::cout << e.what() << std::endl; |
| 106 | + if (e.raw_server_error()) { |
| 107 | + std::cout << bsoncxx::to_json(*(e.raw_server_error())) << std::endl; |
| 108 | + } |
| 109 | + std::cout << std::endl; |
| 110 | + } |
| 111 | + // @end: cpp-operation-exception |
| 112 | + |
| 113 | + // @begin: cpp-bulk-write-exception |
| 114 | + // Adding a document whose "_id" is already present throws a mongocxx::bulk_write_exception. |
| 115 | + // A mongocxx::bulk_write_exception is-a mongocxx::operation_exception is-a mongocxx::exception |
| 116 | + // is-a std::system_error. |
| 117 | + auto doc1 = document{} << "_id" << 1 << finalize; |
| 118 | + coll.insert_one(doc1.view()); |
| 119 | + try { |
| 120 | + coll.insert_one(doc1.view()); |
| 121 | + } catch (const mongocxx::bulk_write_exception& e) { |
| 122 | + std::cout << "Adding a document whose _id is already present throws:" << std::endl; |
| 123 | + |
| 124 | + // We expect this error to arise from the server |
| 125 | + if (e.code().category() != mongocxx::server_error_category()) { |
| 126 | + return EXIT_FAILURE; |
| 127 | + } |
| 128 | + |
| 129 | + // We can compare the error_code to a known server side error code number |
| 130 | + if (e.code().value() != 11000) { |
| 131 | + return EXIT_FAILURE; |
| 132 | + } |
| 133 | + |
| 134 | + std::cout << e.what() << std::endl; |
| 135 | + if (e.raw_server_error()) { |
| 136 | + std::cout << "Raw server error:" << std::endl; |
| 137 | + std::cout << bsoncxx::to_json(*(e.raw_server_error())) << std::endl; |
| 138 | + } |
| 139 | + std::cout << std::endl; |
| 140 | + } |
| 141 | + // @end: cpp-bulk-write-exception |
| 142 | + |
| 143 | + return EXIT_SUCCESS; |
| 144 | +} |
0 commit comments