operation_exception.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2014 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 <bsoncxx/document/value.hpp>
  16. #include <bsoncxx/stdx/optional.hpp>
  17. #include <mongocxx/exception/exception.hpp>
  18. #include <mongocxx/stdx.hpp>
  19. #include <mongocxx/config/prelude.hpp>
  20. namespace mongocxx {
  21. MONGOCXX_INLINE_NAMESPACE_BEGIN
  22. ///
  23. /// Class representing an exception received from a MongoDB server. It includes the server-provided
  24. /// error code, if one was available.
  25. ///
  26. /// @see mongocxx::exception
  27. ///
  28. class MONGOCXX_API operation_exception : public exception {
  29. public:
  30. using exception::exception;
  31. ///
  32. /// Constructs a new operation exception.
  33. ///
  34. /// @param ec
  35. /// The error code associated with this exception.
  36. /// @param raw_server_error
  37. /// The optional raw bson error document to be associated with this exception.
  38. /// @param what_arg
  39. /// An optional message to be returned by `what`.
  40. ///
  41. operation_exception(std::error_code ec,
  42. bsoncxx::document::value&& raw_server_error,
  43. std::string what_arg = "");
  44. ///
  45. /// @{
  46. ///
  47. /// The optional raw bson error document from the server.
  48. ///
  49. /// @returns The raw server error, if it is available.
  50. ///
  51. const stdx::optional<bsoncxx::document::value>& raw_server_error() const;
  52. stdx::optional<bsoncxx::document::value>& raw_server_error();
  53. ///
  54. /// @}
  55. ///
  56. ///
  57. /// Checks for the specified label in a operation exception.
  58. ///
  59. /// @param label
  60. /// A string of the label to search for
  61. ///
  62. /// @returns True if the label is found in the operation exception, and false otherwise.
  63. ///
  64. bool has_error_label(stdx::string_view label) const;
  65. private:
  66. stdx::optional<bsoncxx::document::value> _raw_server_error;
  67. };
  68. MONGOCXX_INLINE_NAMESPACE_END
  69. } // namespace mongocxx
  70. #include <mongocxx/config/postlude.hpp>