error_code.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #pragma once
  15. #include <system_error>
  16. #include <mongocxx/config/prelude.hpp>
  17. namespace mongocxx {
  18. MONGOCXX_INLINE_NAMESPACE_BEGIN
  19. ///
  20. /// Enum representing the various error types that can occur during driver usage.
  21. ///
  22. enum class error_code : std::int32_t {
  23. /// More than one mongocxx::instance has been created.
  24. k_cannot_recreate_instance = 1,
  25. /// A default-constructed or moved-from mongocxx::client object has been used.
  26. k_invalid_client_object,
  27. /// A default-constructed or moved-from mongocxx::collection object has been used.
  28. k_invalid_collection_object,
  29. /// A default-constructed or moved-from mongocxx::database object has been used.
  30. k_invalid_database_object,
  31. /// An invalid or out-of-bounds parameter was provided.
  32. k_invalid_parameter,
  33. /// An SSL operation was used without SSL support being built.
  34. k_ssl_not_supported,
  35. /// An unknown read concern level was set.
  36. k_unknown_read_concern,
  37. /// An unknown write concern level was set.
  38. k_unknown_write_concern,
  39. /// The server returned a malformed response.
  40. k_server_response_malformed,
  41. /// An invalid MongoDB URI was provided.
  42. k_invalid_uri,
  43. /// A default-constructed or moved-from mongocxx::gridfs::bucket object has been used.
  44. k_invalid_gridfs_bucket_object,
  45. /// A default-constructed or moved-from mongocxx::gridfs::uploader object has been used.
  46. k_invalid_gridfs_uploader_object,
  47. /// A default-constructed or moved-from mongocxx::gridfs::downloader object has been used.
  48. k_invalid_gridfs_downloader_object,
  49. /// A mongocxx::gridfs::uploader object was not open for writing, or a
  50. /// mongocxx::gridfs::downloader object was not open for reading.
  51. k_gridfs_stream_not_open,
  52. /// A mongocxx::gridfs::uploader object has exceeded the maximum number of allowable GridFS
  53. /// chunks when attempting to upload the requested file.
  54. k_gridfs_upload_requires_too_many_chunks,
  55. /// The requested GridFS file was not found.
  56. k_gridfs_file_not_found,
  57. /// A GridFS file being operated on was discovered to be corrupted.
  58. k_gridfs_file_corrupted,
  59. /// The mongocxx::instance has been destroyed.
  60. k_instance_destroyed,
  61. /// mongocxx::client.create_session failed to create a mongocxx::client_session.
  62. k_cannot_create_session,
  63. /// A failure attempting to pass a mongocxx::client_session to a method.
  64. k_invalid_session,
  65. /// A moved-from mongocxx::options::transaction object has been used.
  66. k_invalid_transaction_options_object,
  67. // Add new constant string message to error_code.cpp as well!
  68. };
  69. ///
  70. /// Get the error_category for mongocxx library exceptions.
  71. ///
  72. /// @return The mongocxx error_category
  73. ///
  74. MONGOCXX_API const std::error_category& MONGOCXX_CALL error_category();
  75. ///
  76. /// Translate a mongocxx::error_code into a std::error_code.
  77. ///
  78. /// @param error A mongocxx::error_code
  79. ///
  80. /// @return A std::error_code
  81. ///
  82. MONGOCXX_INLINE std::error_code make_error_code(error_code error) {
  83. return {static_cast<int>(error), error_category()};
  84. }
  85. MONGOCXX_INLINE_NAMESPACE_END
  86. } // namespace mongocxx
  87. #include <mongocxx/config/postlude.hpp>
  88. namespace std {
  89. // Specialize is_error_code_enum so we get simpler std::error_code construction
  90. template <>
  91. struct is_error_code_enum<mongocxx::error_code> : public true_type {};
  92. } // namespace std