Skip to content

Commit 5052864

Browse files
Throw YAML::Exception during conversion if the data type mismatches. (#2262) (#2304)
* throw YAML::Exception during conversion if the data type mismatches. * fix build failure for dependent packages. * explicitly handle the decoder exception. --------- (cherry picked from commit 2f89eec) Signed-off-by: Tomoya.Fujita <tomoya.fujita825@gmail.com> Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> Co-authored-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
1 parent 969f153 commit 5052864

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

rosbag2_storage/include/rosbag2_storage/yaml.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ template<typename T>
4242
void optional_assign(const Node & node, std::string field, T & assign_to)
4343
{
4444
if (node[field]) {
45-
YAML::convert<T>::decode(node[field], assign_to);
45+
bool converted = false;
46+
try {
47+
converted = YAML::convert<T>::decode(node[field], assign_to);
48+
} catch (const YAML::Exception & ex) {
49+
// Preserve original error but add field name context
50+
throw YAML::Exception(
51+
node[field].Mark(),
52+
"Failed to convert field '" + field + "': " + ex.what());
53+
}
54+
if (!converted) {
55+
throw YAML::Exception(node[field].Mark(), "Failed to convert field '" + field + "'");
56+
}
4657
}
4758
}
4859

rosbag2_storage/test/rosbag2_storage/test_storage_options.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,19 @@ TEST(storage_options, test_yaml_serialization)
5454
ASSERT_EQ(original.end_time_ns, reconstructed.end_time_ns);
5555
ASSERT_EQ(original.custom_data, reconstructed.custom_data);
5656
}
57+
58+
TEST(storage_options, test_invalid_numeric_value)
59+
{
60+
YAML::Node node;
61+
node["uri"] = "some_uri";
62+
node["storage_id"] = "some_identification";
63+
node["max_bagfile_size"] = "non_numeric_value";
64+
try {
65+
node.as<rosbag2_storage::StorageOptions>();
66+
FAIL() << "Expected YAML::Exception to be thrown";
67+
} catch (const YAML::Exception & ex) {
68+
std::string error_msg = ex.what();
69+
EXPECT_THAT(error_msg, HasSubstr("max_bagfile_size"));
70+
EXPECT_THAT(error_msg, HasSubstr("Failed to convert field"));
71+
}
72+
}

0 commit comments

Comments
 (0)