From 5f42db7d5083015fc498b6bc1c29045e0cf2639d Mon Sep 17 00:00:00 2001 From: Yehor KOROTENKO <60360745+DobbiKov@users.noreply.github.com> Date: Wed, 4 Jun 2025 12:19:11 +0200 Subject: [PATCH] Handle constraint errors without panicking --- src/logger/file_handler/file_manager.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/logger/file_handler/file_manager.rs b/src/logger/file_handler/file_manager.rs index 61ce3df..9f79fd1 100644 --- a/src/logger/file_handler/file_manager.rs +++ b/src/logger/file_handler/file_manager.rs @@ -83,6 +83,8 @@ pub(crate) enum VerifyConstraintsRes { pub(crate) enum WriteLogError { #[error("unable to write to the file: {0}")] UnableToWriteToFile(WriteToFileError), + #[error("constraints verification failed: {0}")] + VerifyConstraintsError(VerifyConstraintsError), } #[derive(Debug, Error)] pub(crate) enum CreateNewFileError { @@ -376,25 +378,23 @@ impl FileManager { mess: &str, config: Config, ) -> Result { - let mut ok_res = Ok(VerifyConstraintsRes::ConstraintsPassed); - match self.verify_constraints(&config) { - Ok(r) => ok_res = Ok(r), + let verify_res = match self.verify_constraints(&config) { + Ok(r) => Ok(r), Err(e) => { eprintln!("An error occured while verifying constraints: {}", e); eprintln!("Trying to write to an old file"); - ok_res = Err(e) + Err(e) } - } + }; let arc_file = self.curr_file.clone(); let mut file = (*arc_file).try_clone().map_err(|e| WriteLogError::UnableToWriteToFile(WriteToFileError::UnexpectedError(e)))?; - - let res = helper::write_to_file(&mut file, mess) - .map(|_| ok_res.unwrap()) - .map_err(WriteLogError::UnableToWriteToFile); + helper::write_to_file(&mut file, mess) + .map_err(WriteLogError::UnableToWriteToFile)?; self.set_curr_file(file); - res + + verify_res.map_err(WriteLogError::VerifyConstraintsError) } }