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