Skip to content

Commit a5b44eb

Browse files
authored
Fix exception thrown given invalid arguments with compression enabled (ros2#488)
* Remove exception throw in dtor Update logging errors to avoid duplication Signed-off-by: Devin Bonnie <dbbonnie@amazon.com> * Cleanup reset logic Add reset documentation Signed-off-by: Devin Bonnie <dbbonnie@amazon.com> * Fix cpplint error Signed-off-by: Devin Bonnie <dbbonnie@amazon.com> * Fix docstring Signed-off-by: Devin Bonnie <dbbonnie@amazon.com>
1 parent 97bf49b commit a5b44eb

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

rosbag2_compression/include/rosbag2_compression/sequential_compression_writer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class ROSBAG2_COMPRESSION_PUBLIC SequentialCompressionWriter
7575
const rosbag2_cpp::StorageOptions & storage_options,
7676
const rosbag2_cpp::ConverterOptions & converter_options) override;
7777

78+
/**
79+
* Attempt to compress the last open file and reset the storage and storage factory.
80+
* This method must be exception safe because it is called by the destructor.
81+
*/
7882
void reset() override;
7983

8084
/**

rosbag2_compression/src/rosbag2_compression/sequential_compression_writer.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ void SequentialCompressionWriter::open(
133133
throw std::runtime_error{"No storage could be initialized. Abort"};
134134
}
135135

136-
if (storage_options.max_bagfile_size != 0 &&
137-
storage_options.max_bagfile_size < storage_->get_minimum_split_file_size())
136+
if (max_bagfile_size_ != 0 &&
137+
max_bagfile_size_ < storage_->get_minimum_split_file_size())
138138
{
139139
std::stringstream error;
140140
error << "Invalid bag splitting size given. Please provide a value greater than " <<
@@ -149,11 +149,7 @@ void SequentialCompressionWriter::open(
149149

150150
void SequentialCompressionWriter::reset()
151151
{
152-
if (!base_folder_.empty()) {
153-
if (!compressor_) {
154-
throw std::runtime_error{"Compressor was not opened!"};
155-
}
156-
152+
if (!base_folder_.empty() && compressor_) {
157153
// Reset may be called before initializing the compressor (ex. bad options).
158154
// We compress the last file only if it hasn't been compressed earlier (ex. in split_bagfile()).
159155
if (compression_options_.compression_mode == rosbag2_compression::CompressionMode::FILE &&
@@ -223,7 +219,7 @@ void SequentialCompressionWriter::remove_topic(
223219
void SequentialCompressionWriter::compress_last_file()
224220
{
225221
if (!compressor_) {
226-
throw std::runtime_error{"Compressor was not opened!"};
222+
throw std::runtime_error{"compress_last_file: Compressor was not opened!"};
227223
}
228224

229225
const auto to_compress = rcpputils::fs::path{metadata_.relative_file_paths.back()};

rosbag2_compression/test/rosbag2_compression/test_sequential_compression_writer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ TEST_F(SequentialCompressionWriterTest, open_throws_on_invalid_splitting_size)
8989
ON_CALL(*storage_, get_minimum_split_file_size()).WillByDefault(Return(min_split_file_size));
9090
auto storage_options = rosbag2_cpp::StorageOptions{};
9191
storage_options.max_bagfile_size = max_bagfile_size;
92+
storage_options.uri = "foo.bar";
9293

9394
auto sequential_writer = std::make_unique<rosbag2_compression::SequentialCompressionWriter>(
9495
compression_options,

0 commit comments

Comments
 (0)