From 7197fabd1e1e6ad8d5d3b676076587c5499a8c9f Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Fri, 27 Sep 2024 16:44:37 -0600 Subject: [PATCH] Working on better error handling. --- FCLauncher/src-tauri/src/admin.rs | 67 ++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/FCLauncher/src-tauri/src/admin.rs b/FCLauncher/src-tauri/src/admin.rs index 79c2e11..935ff5a 100644 --- a/FCLauncher/src-tauri/src/admin.rs +++ b/FCLauncher/src-tauri/src/admin.rs @@ -11,12 +11,9 @@ use crate::modpack::get_versions; use crate::modpack::VersionEntry; use crate::sftp; use crate::modpack; -use crate::ModpackEntry; -use ssh2::Sftp; +use serde::Serialize; use ssh2::Session; use chrono; -use zip::unstable::write::FileOptionsExt; -use zip::write::FileOptions; use zip::write::SimpleFileOptions; use zip::ZipArchive; use zip::ZipWriter; @@ -25,16 +22,22 @@ use zip::ZipWriter; //static PASSWORD: parking_lot::Mutex = parking_lot::const_mutex(String::new()); static SESSION: tauri::async_runtime::Mutex> = tauri::async_runtime::Mutex::const_new(None); +pub fn emit(event: &str, payload: impl Serialize + Clone, window: tauri::Window){ + if !window.emit(event, payload).is_ok() { + println!("Failed to emit to window!"); + } +} + #[tauri::command] pub async fn login(username: String, password: String, window: tauri::Window) { let res = sftp::connect(username, password); - if(res.is_ok()){ + if res.is_ok() { //*USERNAME.lock() = username; //*PASSWORD.lock() = password; *SESSION.lock().await = Some(res.unwrap()); - window.emit("Login_Success", {}); + emit("Login_Success", {}, window); }else{ - window.emit("Login_Failed", {}); + emit("Login_Failed", {}, window); } } @@ -42,7 +45,7 @@ pub async fn login(username: String, password: String, window: tauri::Window) { pub async fn drop_session(){ let ref mut session = *SESSION.lock().await; if let Some(session) = session { - session.disconnect(None, "disconnecting", None); + session.disconnect(None, "disconnecting", None).unwrap(); } *session = None; } @@ -52,9 +55,9 @@ async fn update_modpacks(modpacks: Vec) -> Result<(), Str let reader = Cursor::new(data.as_bytes()); let ref mut session = *SESSION.lock().await; if let Some(session) = session { - sftp::uplaod(None, session.clone(), PathBuf::from("/ftp/modpacks.json"), reader, format!("modpacks.json"), data.as_bytes().len()).await; + sftp::uplaod(None, session.clone(), PathBuf::from("/ftp/modpacks.json"), reader, format!("modpacks.json"), data.as_bytes().len()).await?; } - Ok(()) + Err(format!("Session doesnt exist?")) } async fn update_versions(id: String, versions: Vec) -> Result<(), String>{ @@ -62,17 +65,17 @@ async fn update_versions(id: String, versions: Vec) -> Re let reader = Cursor::new(data.as_bytes()); let ref mut session = *SESSION.lock().await; if let Some(session) = session { - sftp::uplaod(None, session.clone(), PathBuf::from(format!("/ftp/{}/versions.json", id)), reader, format!("modpacks.json"), data.as_bytes().len()).await; + sftp::uplaod(None, session.clone(), PathBuf::from(format!("/ftp/{}/versions.json", id)), reader, format!("modpacks.json"), data.as_bytes().len()).await?; } - Ok(()) + Err(format!("Session doesnt exist?")) } #[tauri::command] -pub async fn shift_up(id: String){ +pub async fn shift_up(id: String, window: tauri::Window){ let mut modpacks = modpack::get_modpacks().await; let mut index = 0; for pack in modpacks.as_slice() { - if(pack.id == id){ + if pack.id == id { break; } index += 1; @@ -80,15 +83,18 @@ pub async fn shift_up(id: String){ if index != 0 { modpacks.swap(index, index-1); } - update_modpacks(modpacks).await; + let res = update_modpacks(modpacks).await; + if !res.is_ok() { + emit("Error", res.unwrap_err(), window); + } } #[tauri::command] -pub async fn shift_down(id: String){ +pub async fn shift_down(id: String, window: tauri::Window){ let mut modpacks = modpack::get_modpacks().await; let mut index = 0; for pack in modpacks.as_slice() { - if(pack.id == id){ + if pack.id == id { break; } index += 1; @@ -96,23 +102,38 @@ pub async fn shift_down(id: String){ if index != modpacks.len()-1 { modpacks.swap(index, index+1); } - update_modpacks(modpacks).await; + let res = update_modpacks(modpacks).await; + if !res.is_ok() { + emit("Error", res.unwrap_err(), window); + } } #[tauri::command] -pub async fn add_pack(id: String, name: String){ +pub async fn add_pack(id: String, name: String, window: tauri::Window){ { let ref mut session = *SESSION.lock().await; if let Some(session) = session{ - sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}", id))).await; - sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}/Versions", id))).await; + let res = sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}", id))).await; + if !res.is_ok() { + emit("Error", res.unwrap_err(), window.clone()); + } + let res = sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}/Versions", id))).await; + if !res.is_ok() { + emit("Error", res.unwrap_err(), window.clone()); + } } } let versions: Vec = Vec::new(); - update_versions(id.clone(), versions).await; + let res = update_versions(id.clone(), versions).await; + if !res.is_ok() { + emit("Error", res.unwrap_err(), window.clone()); + } let mut modpacks = get_modpacks().await; modpacks.push(modpack::ModpackEntry{id: id, name: name, last_updated: format!("{:?}", chrono::offset::Utc::now())}); - update_modpacks(modpacks).await; + let res = update_modpacks(modpacks).await; + if !res.is_ok() { + emit("Error", res.unwrap_err(), window); + } } #[tauri::command]