Test for JSON & CBOR encoding error handling (#432)

This commit is contained in:
Bryce Schober
2025-05-14 22:06:36 +08:00
committed by GitHub
parent 778cd54173
commit b6446a8be4
5 changed files with 169 additions and 13 deletions
+20 -6
View File
@@ -20,17 +20,31 @@ using InputVarType = typename Reader::InputVarType;
/// Parses an object from CBOR using reflection.
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(const std::vector<char>& _bytes) {
auto val = jsoncons::cbor::decode_cbor<jsoncons::json>(_bytes);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
// TODO: Use a non-throwing decode_cbor(), pending https://github.com/danielaparker/jsoncons/issues/615
try {
auto val = jsoncons::cbor::decode_cbor<jsoncons::json>(_bytes);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
} catch(const jsoncons::ser_error& e) {
std::string error("Could not parse CBOR: ");
error.append(e.what());
return rfl::error(error);
}
}
/// Parses an object from a stream.
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
auto val = jsoncons::cbor::decode_cbor<jsoncons::json>(_stream);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
// TODO: Use a non-throwing decode_cbor(), pending https://github.com/danielaparker/jsoncons/issues/615
try {
auto val = jsoncons::cbor::decode_cbor<jsoncons::json>(_stream);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
} catch(const jsoncons::ser_error& e) {
std::string error("Could not parse CBOR: ");
error.append(e.what());
return rfl::error(error);
}
}
} // namespace rfl::cbor
+20 -6
View File
@@ -20,17 +20,31 @@ using InputVarType = typename Reader::InputVarType;
/// Parses an object from UBJSON using reflection.
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(const std::vector<char>& _bytes) {
auto val = jsoncons::ubjson::decode_ubjson<jsoncons::json>(_bytes);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
// TODO: Use a non-throwing decode_ubjson(), pending https://github.com/danielaparker/jsoncons/issues/615
try {
auto val = jsoncons::ubjson::decode_ubjson<jsoncons::json>(_bytes);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
} catch(const jsoncons::ser_error& e) {
std::string error("Could not parse UBJSON: ");
error.append(e.what());
return rfl::error(error);
}
}
/// Parses an object from a stream.
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(std::istream& _stream) {
auto val = jsoncons::ubjson::decode_ubjson<jsoncons::json>(_stream);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
// TODO: Use a non-throwing decode_ubjson(), pending https://github.com/danielaparker/jsoncons/issues/615
try {
auto val = jsoncons::ubjson::decode_ubjson<jsoncons::json>(_stream);
auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, InputVarType{&val});
} catch(const jsoncons::ser_error& e) {
std::string error("Could not parse UBJSON: ");
error.append(e.what());
return rfl::error(error);
}
}
} // namespace rfl::ubjson
+56
View File
@@ -0,0 +1,56 @@
#include <gtest/gtest.h>
#include <iostream>
#include <rfl.hpp>
#include <rfl/cbor.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
namespace test_error_messages {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
std::vector<Person> children;
};
TEST(cbor, test_field_error_messages) {
const std::string faulty_string =
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
const auto faulty_generic = rfl::json::read<rfl::Generic>(faulty_string);
const auto faulty_cbor = rfl::cbor::write(faulty_generic);
const auto result = rfl::cbor::read<Person>(faulty_cbor);
// Order of errors is different than input JSON because rfl::Generic doesn't preserve order
const std::string expected = R"(Found 3 errors:
1) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'.
2) Failed to parse field 'lastName': Could not cast to string.
3) Field named 'children' not found.)";
EXPECT_TRUE(!result.has_value() && true);
EXPECT_EQ(result.error().what(), expected);
}
TEST(cbor, test_decode_error_without_exception) {
const std::string good_string =
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})";
const auto good_generic = rfl::json::read<rfl::Generic>(good_string);
auto faulty_cbor = rfl::cbor::write(good_generic);
faulty_cbor[1] = '\xff'; // Corrupt structure of CBOR encoding
rfl::Result<Person> result = rfl::error("result didn't get set");
EXPECT_NO_THROW({
result = rfl::cbor::read<Person>(faulty_cbor);
});
// A proposal: A generic prefix, followed by the underlying library's error output
const std::string expected = R"(Could not parse CBOR: An unknown type was found in the stream at position 1)";
EXPECT_EQ(result.error().what(), expected);
EXPECT_TRUE(!result.has_value() && true);
}
} // namespace test_error_messages
+17 -1
View File
@@ -15,7 +15,7 @@ struct Person {
std::vector<Person> children;
};
TEST(json, test_error_messages) {
TEST(json, test_field_error_messages) {
const std::string faulty_string =
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
@@ -30,4 +30,20 @@ TEST(json, test_error_messages) {
EXPECT_EQ(result.error().what(), expected);
}
TEST(json, test_decode_error_without_exception) {
// Note using valid field names, but invalid JSON delimiter ';'
const std::string faulty_string =
R"({"firstName":"Homer";"lastName":"Simpson";"birthday":"1987-04-19"})";
rfl::Result<Person> result = rfl::error("result didn't get set");
EXPECT_NO_THROW({
result = rfl::json::read<Person>(faulty_string);
});
EXPECT_TRUE(!result.has_value() && true);
EXPECT_EQ(result.error().what(), "Could not parse document");
}
} // namespace test_error_messages
+56
View File
@@ -0,0 +1,56 @@
#include <gtest/gtest.h>
#include <iostream>
#include <rfl.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
namespace test_error_messages {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
std::vector<Person> children;
};
TEST(ubjson, test_field_error_messages) {
const std::string faulty_string =
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
const auto faulty_generic = rfl::json::read<rfl::Generic>(faulty_string);
const auto faulty_ubjson = rfl::ubjson::write(faulty_generic);
const auto result = rfl::ubjson::read<Person>(faulty_ubjson);
// Order of errors is different than input JSON because rfl::Generic doesn't preserve order
const std::string expected = R"(Found 3 errors:
1) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'.
2) Failed to parse field 'lastName': Could not cast to string.
3) Field named 'children' not found.)";
EXPECT_TRUE(!result.has_value() && true);
EXPECT_EQ(result.error().what(), expected);
}
TEST(ubjson, test_decode_error_without_exception) {
const std::string good_string =
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})";
const auto good_generic = rfl::json::read<rfl::Generic>(good_string);
auto faulty_ubjson = rfl::ubjson::write(good_generic);
faulty_ubjson[0] = '\xff'; // Corrupt structure of ubjson encoding
rfl::Result<Person> result = rfl::error("result didn't get set");
EXPECT_NO_THROW({
result = rfl::ubjson::read<Person>(faulty_ubjson);
});
// A proposal: A generic prefix, followed by the underlying library's error output
const std::string expected = R"(Could not parse UBJSON: Unknown type at position 1)";
EXPECT_EQ(result.error().what(), expected);
EXPECT_TRUE(!result.has_value() && true);
}
} // namespace test_error_messages