error_code.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <cstdint>
  16. #include <system_error>
  17. #include <bsoncxx/config/prelude.hpp>
  18. namespace bsoncxx {
  19. BSONCXX_INLINE_NAMESPACE_BEGIN
  20. ///
  21. /// Enum representing the various error types that can occur while operating on BSON values.
  22. ///
  23. enum class error_code : std::int32_t {
  24. /// A new key was appended while building a subarray.
  25. k_cannot_append_key_in_sub_array = 1,
  26. /// A subarray was closed while building a subdocument.
  27. k_cannot_close_array_in_sub_document,
  28. /// A subdocument was closed while building a subarray.
  29. k_cannot_close_document_in_sub_array,
  30. /// An array operation was performed while building a document.
  31. k_cannot_perform_array_operation_on_document,
  32. /// A document operation was performed while building an array.
  33. k_cannot_perform_document_operation_on_array,
  34. #define BSONCXX_ENUM(name, value) k_need_element_type_k_##name,
  35. #include <bsoncxx/enums/type.hpp>
  36. #undef BSONCXX_ENUM
  37. /// No key was provided when one was needed.
  38. k_need_key,
  39. /// An array was closed while no array was open.
  40. k_no_array_to_close,
  41. /// A document was closed while no document was open.
  42. k_no_document_to_close,
  43. // Attempted to view or extract a document when a key was still awaiting a matching value.
  44. k_unmatched_key_in_builder,
  45. /// An empty element was accessed.
  46. k_unset_element,
  47. /// A JSON document failed to parse.
  48. k_json_parse_failure,
  49. /// An Object ID string failed to parse.
  50. k_invalid_oid,
  51. // This type is unused and deprecated.
  52. k_failed_converting_bson_to_json,
  53. /// A Decimal128 string failed to parse.
  54. k_invalid_decimal128,
  55. /// BSON data could not be processed, but no specific reason was available.
  56. k_internal_error,
  57. /// Failed to begin appending an array to a BSON document or array.
  58. k_cannot_begin_appending_array,
  59. /// Failed to begin appending a BSON document to a BSON document or array.
  60. k_cannot_begin_appending_document,
  61. /// Failed to complete appending an array to a BSON document or array.
  62. k_cannot_end_appending_array,
  63. /// Failed to complete appending a BSON document to a BSON document or array.
  64. k_cannot_end_appending_document,
  65. /// Invalid binary subtype.
  66. k_invalid_binary_subtype,
  67. /// A value failed to append.
  68. #define BSONCXX_ENUM(name, value) k_cannot_append_##name,
  69. #include <bsoncxx/enums/type.hpp>
  70. #undef BSONCXX_ENUM
  71. // Add new constant string message to error_code.cpp as well!
  72. };
  73. ///
  74. /// Get the error_category for exceptions originating from the bsoncxx library.
  75. ///
  76. /// @return The bsoncxx error_category
  77. ///
  78. BSONCXX_API const std::error_category& BSONCXX_CALL error_category();
  79. ///
  80. /// Translate a bsoncxx::error_code into a std::error_code.
  81. ///
  82. /// @param error An error from bsoncxx
  83. /// @return An error_code
  84. ///
  85. BSONCXX_INLINE std::error_code make_error_code(error_code error) {
  86. return {static_cast<int>(error), error_category()};
  87. }
  88. BSONCXX_INLINE_NAMESPACE_END
  89. } // namespace bsoncxx
  90. #include <bsoncxx/config/postlude.hpp>
  91. namespace std {
  92. // Specialize is_error_code_enum so we get simpler std::error_code construction
  93. template <>
  94. struct is_error_code_enum<bsoncxx::error_code> : public true_type {};
  95. } // namespace std