diff --git a/rosbag2_cpp/src/rosbag2_cpp/message_definitions/local_message_definition_source.cpp b/rosbag2_cpp/src/rosbag2_cpp/message_definitions/local_message_definition_source.cpp index 15b68ce106..3382efb6a7 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/message_definitions/local_message_definition_source.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/message_definitions/local_message_definition_source.cpp @@ -24,6 +24,7 @@ #include #include +#include #include "rosbag2_cpp/logging.hpp" @@ -179,7 +180,13 @@ const LocalMessageDefinitionSource::MessageSpec & LocalMessageDefinitionSource:: throw TypenameNotUnderstoodError(topic_type); } std::string package = match[1]; - std::string share_dir = ament_index_cpp::get_package_share_directory(package); + std::string share_dir; + try { + share_dir = ament_index_cpp::get_package_share_directory(package); + } catch (const ament_index_cpp::PackageNotFoundError & e) { + ROSBAG2_CPP_LOG_WARN("'%s'", e.what()); + throw DefinitionNotFoundError(definition_identifier.topic_type()); + } std::string dir = definition_identifier.format() == Format::MSG || definition_identifier.format() == Format::IDL ? "/msg/" : "/srv/"; std::ifstream file{share_dir + dir + match[2].str() + diff --git a/rosbag2_cpp/test/rosbag2_cpp/test_local_message_definition_source.cpp b/rosbag2_cpp/test/rosbag2_cpp/test_local_message_definition_source.cpp index e9dcc658df..710f99b225 100644 --- a/rosbag2_cpp/test/rosbag2_cpp/test_local_message_definition_source.cpp +++ b/rosbag2_cpp/test/rosbag2_cpp/test_local_message_definition_source.cpp @@ -181,3 +181,18 @@ TEST(test_local_message_definition_source, no_crash_on_bad_name) }); ASSERT_EQ(result.encoding, "unknown"); } + +TEST(test_local_message_definition_source, throw_definition_not_found_for_unknown_msg) +{ + LocalMessageDefinitionSource source; + ASSERT_THROW( + { + source.get_full_text("rosbag2_test_msgdefs/msg/UnknownMessage"); + }, rosbag2_cpp::DefinitionNotFoundError); + + // Throw DefinitionNotFoundError for not found message definition package name + ASSERT_THROW( + { + source.get_full_text("not_found_msgdefs_pkg/msg/UnknownMessage"); + }, rosbag2_cpp::DefinitionNotFoundError); +} diff --git a/rosbag2_transport/src/rosbag2_transport/bag_rewrite.cpp b/rosbag2_transport/src/rosbag2_transport/bag_rewrite.cpp index db94ccd877..7331ab7770 100644 --- a/rosbag2_transport/src/rosbag2_transport/bag_rewrite.cpp +++ b/rosbag2_transport/src/rosbag2_transport/bag_rewrite.cpp @@ -111,7 +111,7 @@ setup_topic_filtering( } for (const auto & [writer, record_options] : output_bags) { - rosbag2_transport::TopicFilter topic_filter{record_options}; + rosbag2_transport::TopicFilter topic_filter{record_options, nullptr, true}; auto filtered_topics_and_types = topic_filter.filter_topics(input_topics); // Done filtering - set up writer