diff --git a/services/backup/blob_client/src/get_client.rs b/services/backup/blob_client/src/get_client.rs --- a/services/backup/blob_client/src/get_client.rs +++ b/services/backup/blob_client/src/get_client.rs @@ -6,7 +6,7 @@ use proto::GetRequest; use crate::constants::{BLOB_ADDRESS, MPSC_CHANNEL_BUFFER_CAPACITY}; -use crate::tools::report_error; +use crate::tools::{report_error, check_error}; use lazy_static::lazy_static; use libc; use libc::c_char; @@ -23,7 +23,8 @@ lazy_static! { static ref CLIENT: Arc>> = - Arc::new(Mutex::new(None)); // todo we should probably create separate clients for different IDs + Arc::new(Mutex::new(None)); + // todo we should probably create separate clients for different IDs static ref RUNTIME: Runtime = Runtime::new().unwrap(); static ref ERROR_MESSAGES: Arc>> = Arc::new(Mutex::new(Vec::new())); @@ -40,16 +41,6 @@ false } -fn check_error() -> Result<(), String> { - if let Ok(errors) = ERROR_MESSAGES.lock() { - return match errors.is_empty() { - true => Ok(()), - false => Err(errors.join("\n")), - }; - } - Err("could not access error messages".to_string()) -} - pub fn get_client_initialize_cxx( holder_char: *const c_char, ) -> Result<(), String> { @@ -120,7 +111,7 @@ pub fn get_client_blocking_read_cxx() -> Result, String> { let mut response: Option> = None; - check_error()?; + check_error(&ERROR_MESSAGES)?; RUNTIME.block_on(async { if let Ok(mut maybe_client) = CLIENT.lock() { if let Some(mut client) = (*maybe_client).take() { @@ -137,13 +128,13 @@ report_error(&ERROR_MESSAGES, "couldn't access client".to_string()); } }); - check_error()?; + check_error(&ERROR_MESSAGES)?; let response: Vec = response.unwrap(); Ok(response) } pub fn get_client_terminate_cxx() -> Result<(), String> { - check_error()?; + check_error(&ERROR_MESSAGES)?; if !is_initialized() { return Ok(()); } @@ -168,6 +159,6 @@ !is_initialized(), "client transmitter handler released properly" ); - check_error()?; + check_error(&ERROR_MESSAGES)?; Ok(()) } diff --git a/services/backup/blob_client/src/put_client.rs b/services/backup/blob_client/src/put_client.rs --- a/services/backup/blob_client/src/put_client.rs +++ b/services/backup/blob_client/src/put_client.rs @@ -9,7 +9,7 @@ use proto::PutResponse; use crate::constants::{BLOB_ADDRESS, MPSC_CHANNEL_BUFFER_CAPACITY}; -use crate::tools::report_error; +use crate::tools::{report_error, check_error}; use lazy_static::lazy_static; use libc; use libc::c_char; @@ -52,16 +52,6 @@ false } -fn check_error() -> Result<(), String> { - if let Ok(errors) = ERROR_MESSAGES.lock() { - return match errors.is_empty() { - true => Ok(()), - false => Err(errors.join("\n")), - }; - } - Err("could not access error messages".to_string()) -} - pub fn put_client_initialize_cxx() -> Result<(), String> { if is_initialized() { put_client_terminate_cxx()?; @@ -187,7 +177,7 @@ pub fn put_client_blocking_read_cxx() -> Result { let mut response: Option = None; - check_error()?; + check_error(&ERROR_MESSAGES)?; RUNTIME.block_on(async { if let Ok(mut maybe_client) = CLIENT.lock() { if let Some(mut client) = (*maybe_client).take() { @@ -207,7 +197,7 @@ report_error(&ERROR_MESSAGES, "couldn't access client".to_string()); } }); - check_error()?; + check_error(&ERROR_MESSAGES)?; response.ok_or("response not received properly".to_string()) } @@ -221,7 +211,7 @@ field_index: usize, data: *const c_char, ) -> Result<(), String> { - check_error()?; + check_error(&ERROR_MESSAGES)?; let data_c_str: &CStr = unsafe { CStr::from_ptr(data) }; let data_bytes: Vec = data_c_str.to_bytes().to_vec(); @@ -254,7 +244,7 @@ } pub fn put_client_terminate_cxx() -> Result<(), String> { - check_error()?; + check_error(&ERROR_MESSAGES)?; if !is_initialized() { return Ok(()); } @@ -281,6 +271,6 @@ !is_initialized(), "client transmitter handler released properly" ); - check_error()?; + check_error(&ERROR_MESSAGES)?; Ok(()) } diff --git a/services/backup/blob_client/src/tools.rs b/services/backup/blob_client/src/tools.rs --- a/services/backup/blob_client/src/tools.rs +++ b/services/backup/blob_client/src/tools.rs @@ -8,3 +8,13 @@ } error!("could not access error messages"); } + +pub fn check_error(error_messages: &Arc>>) -> Result<(), String> { + if let Ok(errors) = error_messages.lock() { + return match errors.is_empty() { + true => Ok(()), + false => Err(errors.join("\n")), + }; + } + Err("could not access error messages".to_string()) +}