Skip to content

Commit cadbc0a

Browse files
authored
CXX-3238 propagate the entire mongoc reply document to v_noabi::operation_exception (#1545)
1 parent 23191e0 commit cadbc0a

File tree

4 files changed

+15
-56
lines changed

4 files changed

+15
-56
lines changed

src/mongocxx/lib/mongocxx/v1/exception.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <bsoncxx/v1/array/value.hpp>
2020
#include <bsoncxx/v1/document/value.hpp>
2121
#include <bsoncxx/v1/document/view.hpp>
22+
#include <bsoncxx/v1/stdx/optional.hpp>
2223
#include <bsoncxx/v1/stdx/string_view.hpp>
2324
#include <bsoncxx/v1/types/id.hpp>
2425
#include <bsoncxx/v1/types/view.hpp>
@@ -185,9 +186,9 @@ std::error_category const& type_error_category() {
185186
class exception::impl {
186187
public:
187188
bsoncxx::v1::array::value _error_labels;
188-
bsoncxx::v1::array::value _write_concern_errors;
189-
bsoncxx::v1::array::value _write_errors;
190-
bsoncxx::v1::array::value _error_replies;
189+
190+
// For backward compatibility with v_noabi::operation_exception.
191+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value> _reply;
191192
};
192193

193194
bool exception::has_error_label(bsoncxx::v1::stdx::string_view label) const {
@@ -332,32 +333,16 @@ void exception::internal::set_error_labels(exception& self, bsoncxx::v1::documen
332333
set_array_field("errorLabels", self._impl->_error_labels, v);
333334
}
334335

335-
void exception::internal::set_write_concern_errors(exception& self, bsoncxx::v1::document::view v) {
336-
set_array_field("writeConcernErrors", self._impl->_write_concern_errors, v);
337-
}
338-
339-
void exception::internal::set_write_errors(exception& self, bsoncxx::v1::document::view v) {
340-
set_array_field("writeErrors", self._impl->_write_errors, v);
341-
}
342-
343-
void exception::internal::set_error_replies(exception& self, bsoncxx::v1::document::view v) {
344-
set_array_field("errorReplies", self._impl->_error_replies, v);
336+
void exception::internal::set_reply(exception& self, bsoncxx::v1::document::value v) {
337+
self._impl->_reply = std::move(v);
345338
}
346339

347340
bsoncxx::v1::array::view exception::internal::get_error_labels(exception const& self) {
348341
return self._impl->_error_labels;
349342
}
350343

351-
bsoncxx::v1::array::view exception::internal::get_write_concern_errors(exception const& self) {
352-
return self._impl->_write_concern_errors;
353-
}
354-
355-
bsoncxx::v1::array::view exception::internal::get_write_errors(exception const& self) {
356-
return self._impl->_write_errors;
357-
}
358-
359-
bsoncxx::v1::array::view exception::internal::get_error_replies(exception const& self) {
360-
return self._impl->_error_replies;
344+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value> const& exception::internal::get_reply(exception const& self) {
345+
return self._impl->_reply;
361346
}
362347

363348
void throw_exception(bson_error_t const& error) {
@@ -377,9 +362,7 @@ void throw_exception(bson_error_t const& error, bsoncxx::v1::document::value doc
377362
auto ex = make_exception(error);
378363

379364
exception::internal::set_error_labels(ex, doc);
380-
exception::internal::set_write_concern_errors(ex, doc);
381-
exception::internal::set_write_errors(ex, doc);
382-
exception::internal::set_error_replies(ex, doc);
365+
exception::internal::set_reply(ex, std::move(doc));
383366

384367
throw std::move(ex);
385368
}

src/mongocxx/lib/mongocxx/v1/exception.hh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <bsoncxx/v1/detail/type_traits.hpp>
2222
#include <bsoncxx/v1/document/value.hpp>
2323
#include <bsoncxx/v1/document/view.hpp>
24+
#include <bsoncxx/v1/stdx/optional.hpp>
2425

2526
#include <system_error>
2627
#include <utility>
@@ -57,14 +58,10 @@ class exception::internal {
5758
}
5859

5960
static MONGOCXX_ABI_EXPORT_CDECL_TESTING(void) set_error_labels(exception& self, bsoncxx::v1::document::view v);
60-
static void set_write_concern_errors(exception& self, bsoncxx::v1::document::view v);
61-
static void set_write_errors(exception& self, bsoncxx::v1::document::view v);
62-
static void set_error_replies(exception& self, bsoncxx::v1::document::view v);
61+
static void set_reply(exception& self, bsoncxx::v1::document::value v);
6362

6463
static bsoncxx::v1::array::view get_error_labels(exception const& self);
65-
static bsoncxx::v1::array::view get_write_concern_errors(exception const& self);
66-
static bsoncxx::v1::array::view get_write_errors(exception const& self);
67-
static bsoncxx::v1::array::view get_error_replies(exception const& self);
64+
static bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value> const& get_reply(exception const& self);
6865
};
6966

7067
[[noreturn]] MONGOCXX_ABI_EXPORT_CDECL_TESTING(void) throw_exception(bson_error_t const& error);

src/mongocxx/lib/mongocxx/v1/server_error.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ bsoncxx::v1::document::view server_error::raw() const {
9797
server_error::server_error(int code, char const* message, std::unique_ptr<impl> impl)
9898
: v1::exception{v1::exception::internal::make(code, server_error_category(), message)}, _impl{std::move(impl)} {
9999
v1::exception::internal::set_error_labels(*this, _impl->_raw);
100-
v1::exception::internal::set_write_concern_errors(*this, _impl->_raw);
101-
v1::exception::internal::set_write_errors(*this, _impl->_raw);
102-
v1::exception::internal::set_error_replies(*this, _impl->_raw);
103100
}
104101

105102
server_error server_error::internal::make(int code, char const* message, bsoncxx::v1::document::value raw) {

src/mongocxx/lib/mongocxx/v_noabi/mongocxx/mongoc_error.hh

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#pragma once
1616

17-
#include <bsoncxx/v1/array/view.hpp>
1817
#include <bsoncxx/v1/detail/type_traits.hpp>
1918
#include <bsoncxx/v1/stdx/string_view.hpp>
2019

@@ -36,8 +35,6 @@
3635

3736
#include <bsoncxx/private/bson.hh>
3837

39-
#include <mongocxx/private/scoped_bson.hh>
40-
4138
namespace mongocxx {
4239
namespace v_noabi {
4340

@@ -118,24 +115,9 @@ template <
118115
strip_ec_msg(ptr->what(), ptr->code()).c_str()};
119116
}
120117

121-
// Array fields must be represented as "raw server error" document fields.
122-
{
123-
scoped_bson doc;
124-
125-
auto const append_array_field = [&](char const* name, bsoncxx::v1::array::view field) {
126-
if (!field.empty()) {
127-
doc += scoped_bson{BCON_NEW(name, BCON_ARRAY(scoped_bson_view{field}.bson()))};
128-
}
129-
};
130-
131-
append_array_field("errorLabels", v1::exception::internal::get_error_labels(ex));
132-
append_array_field("writeConcernErrors", v1::exception::internal::get_write_concern_errors(ex));
133-
append_array_field("writeErrors", v1::exception::internal::get_write_errors(ex));
134-
append_array_field("errorReplies", v1::exception::internal::get_error_replies(ex));
135-
136-
if (!doc.view().empty()) {
137-
throw exception_type{code, from_v1(std::move(doc).value()), strip_ec_msg(ex.what(), ex.code()).c_str()};
138-
}
118+
// Propagate the original mongoc reply document as the "raw server error" document.
119+
if (auto const& reply = v1::exception::internal::get_reply(ex)) {
120+
throw exception_type{code, from_v1(*reply), strip_ec_msg(ex.what(), ex.code()).c_str()};
139121
}
140122

141123
// No "raw server error" document is required.

0 commit comments

Comments
 (0)