feat(errors): Using SerializeDisplay for better error management with Result

This commit is contained in:
quexeky
2025-01-19 17:17:51 +11:00
parent c2f54c1dbc
commit 170fde5e23
15 changed files with 74 additions and 107 deletions

View File

@ -6,7 +6,7 @@ use std::{
use serde_json::Value;
use crate::{database::settings::Settings, error::user_error::UserValue, DB};
use crate::{database::settings::Settings, download_manager::{download_manager::DownloadManagerSignal, internal_error::InternalError}, DB};
use super::{db::DATA_ROOT_DIR, debug::SystemData};
@ -27,16 +27,16 @@ pub fn delete_download_dir(index: usize) {
}
#[tauri::command]
pub fn add_download_dir(new_dir: PathBuf) -> UserValue<(), Error> {
pub fn add_download_dir(new_dir: PathBuf) -> Result<(), InternalError<()>> {
// Check the new directory is all good
let new_dir_path = Path::new(&new_dir);
if new_dir_path.exists() {
let dir_contents = new_dir_path.read_dir()?;
if dir_contents.count() != 0 {
return UserValue::Err(Error::new(
return Err(Error::new(
ErrorKind::DirectoryNotEmpty,
"Selected directory cannot contain any existing files",
));
).into());
}
} else {
create_dir_all(new_dir_path)?;
@ -45,16 +45,16 @@ pub fn add_download_dir(new_dir: PathBuf) -> UserValue<(), Error> {
// Add it to the dictionary
let mut lock = DB.borrow_data_mut().unwrap();
if lock.applications.install_dirs.contains(&new_dir) {
return UserValue::Err(Error::new(
return Err(Error::new(
ErrorKind::AlreadyExists,
"Selected directory already exists in database",
));
).into());
}
lock.applications.install_dirs.push(new_dir);
drop(lock);
DB.save().unwrap();
UserValue::Ok(())
Ok(())
}
#[tauri::command]