How to handle BSON document failures, errors, and exceptions.
Create a BSON Document
From an Invalid JSON String
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_json_parse_failure);
}
}
Using the Basic Document Builder
Basic Append Failure
void example(bsoncxx::stdx::string_view big_string) {
builder.append(
kvp(
"key",
"value"));
try {
builder.append(
kvp(
"too big", big_string));
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string);
}
EXPECT(builder.view() == original.view());
}
Sub-Document Append Failure
void example(bsoncxx::stdx::string_view big_string) {
builder.append(
kvp(
"key",
"value"));
try {
doc.append(
kvp(
"too big", big_string));
}));
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string);
}
try {
builder.view();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder);
}
builder.clear();
EXPECT(builder.view().empty());
EXPECT(builder.view() == original.view());
}
Sub-Array Append Failure
void example(bsoncxx::stdx::string_view big_string) {
builder.append(
kvp(
"key",
"value"));
try {
arr.append(big_string);
}));
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string);
}
try {
builder.view();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder);
}
builder.clear();
EXPECT(builder.view().empty());
EXPECT(builder.view() == original.view());
}
Using the Basic Array Builder
Basic Append Failure
void example(bsoncxx::stdx::string_view big_string) {
builder.append("element");
try {
builder.append(big_string);
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string);
}
EXPECT(builder.view() == original.view());
}
Sub-Document Append Failure
void example(bsoncxx::stdx::string_view big_string) {
builder.append("element");
try {
doc.append(
kvp(
"too big", big_string));
});
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string);
}
try {
builder.view();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder);
}
builder.clear();
EXPECT(builder.view().empty());
EXPECT(builder.view() == original.view());
}
Sub-Array Append Failure
void example(bsoncxx::stdx::string_view big_string) {
builder.append("element");
try {
arr.append(big_string);
});
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string);
}
try {
builder.view();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder);
}
builder.clear();
EXPECT(builder.view().empty());
EXPECT(builder.view() == original.view());
}
Access a Document Element
By Iteration
End Iterators
EXPECT(doc.begin() != doc.end());
auto iter = doc.begin();
++iter;
++iter;
EXPECT(iter == doc.end());
++iter;
EXPECT(iter == doc.end());
EXPECT(!e);
}
Invalid BSON Documents
void example() {
bsoncxx::document::value::deleter_type deleter = [](std::uint8_t*) {};
std::uint8_t data[] = {0u};
auto iter = doc.begin();
EXPECT(iter == doc.end());
EXPECT(!e);
}
By Key
Missing Element
EXPECT(doc["a"]);
EXPECT(doc["b"]);
EXPECT(!e);
}
Access an Array Element
By Iteration
End Iterators
EXPECT(arr.begin() != arr.end());
auto iter = arr.begin();
++iter;
++iter;
++iter;
EXPECT(iter == arr.end());
++iter;
EXPECT(iter == arr.end());
EXPECT(!e);
}
Invalid BSON Arrays
void example() {
bsoncxx::array::value::deleter_type deleter = [](std::uint8_t*) {};
std::uint8_t data[] = {0u};
auto iter = arr.begin();
EXPECT(iter == arr.end());
EXPECT(!e);
}
By Key
Missing Element
EXPECT(std::distance(arr.begin(), arr.end()) == 3);
EXPECT(!e);
}
Query an Element
In a Document
Invalid Element
void example() {
bsoncxx::document::value::deleter_type deleter = [](std::uint8_t*) {};
std::uint8_t data[] = {0u};
EXPECT(!e);
try {
bsoncxx::stdx::string_view key = e.key();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unset_element);
}
try {
bsoncxx::type
type = e.type();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unset_element);
}
}
Invalid Type
EXPECT(e.key() == "x");
EXPECT(e.type() == bsoncxx::type::k_int32);
EXPECT(e.get_int32().value == 1);
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double);
}
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string);
}
}
In an Array
Invalid Element
void example() {
bsoncxx::array::value::deleter_type deleter = [](std::uint8_t*) {};
std::uint8_t data[] = {0u};
EXPECT(!e);
try {
bsoncxx::stdx::string_view key = e.key();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unset_element);
}
try {
bsoncxx::type
type = e.type();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unset_element);
}
}
Invalid Type
EXPECT(e.key() == "0");
EXPECT(e.type() == bsoncxx::type::k_int32);
EXPECT(e.get_int32().value == 1);
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double);
}
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string);
}
}
Use a BSON Value
Query an Invalid Type
void example() {
EXPECT(v.type() == bsoncxx::type::k_null);
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int32);
}
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int64);
}
}
Create an Invalid Value
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_invalid_bson_type_id);
}
}
From an Invalid Element
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_unset_element);
}
}
Convert to a JSON String
From an Invalid BSON Document
void example() {
bsoncxx::document::value::deleter_type deleter = [](std::uint8_t*) {};
std::uint8_t data[] = {0u};
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json);
}
}
From an Invalid BSON Array
void example() {
bsoncxx::array::value::deleter_type deleter = [](std::uint8_t*) {};
std::uint8_t data[] = {0u};
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json);
}
}