Skip to content

Commit

Permalink
Improve error handling in file operations
Browse files Browse the repository at this point in the history
  • Loading branch information
umegane committed Aug 29, 2024
1 parent 1e41608 commit 096c67d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/limestone/log_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,16 @@ rotation_result log_channel::do_rotate_file(epoch_id_type epoch) {
<< "." << epoch;
std::string new_name = ss.str();
boost::filesystem::path new_file = location_ / new_name;
boost::filesystem::rename(file_path(), new_file);

// Rename the file and handle any errors
boost::system::error_code error;
boost::filesystem::rename(file_path(), new_file, error);
if (error) {
LOG_LP(ERROR) << "Failed to rename file: " << file_path() << " to " << new_file
<< ", error_code: " << error.message();
throw std::runtime_error("Failed to rename file: " + file_path().string());
}

envelope_.add_file(new_file);

registered_ = false;
Expand Down
15 changes: 12 additions & 3 deletions src/limestone/rotation_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <limestone/logging.h>

#include <glog/logging.h>
#include <limestone/logging.h>

Check warning on line 19 in src/limestone/rotation_task.cpp

View workflow job for this annotation

GitHub Actions / Clang-Tidy

readability-duplicate-include

duplicate include
#include "logging_helper.h"

#include <limestone/api/rotation_task.h>
#include <limestone/api/datastore.h>
Expand Down Expand Up @@ -59,14 +64,18 @@ void rotation_result::add_rotation_result(const rotation_result& other) {
rotation_task::rotation_task(datastore& envelope)
: envelope_(envelope), result_future_(result_promise_.get_future()) {}


void rotation_task::rotate() {
rotation_result final_result;
for (const auto& lc : envelope_.log_channels_) {
boost::system::error_code error;
bool result = boost::filesystem::exists(lc->file_path(), error);
if (!result || error) {
continue; // skip if not exists
if (error) {
LOG_LP(ERROR) << "Failed to check if file exists: " << lc->file_path() << ", error_code: " << error.message();
throw std::runtime_error("Failed to check if file exists: " + lc->file_path().string());
}
if (!result) {
LOG_LP(INFO) << "File does not exist, skipping: " << lc->file_path();
continue; // skip if file does not exist
}
// The following code may seem necessary at first glance, but there is a possibility
// that files could be appended to before the rotation is complete.
Expand Down

0 comments on commit 096c67d

Please sign in to comment.