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

@ -4,7 +4,7 @@ use tauri::AppHandle;
use crate::{
error::{
library_error::LibraryError, remote_access_error::RemoteAccessError, user_error::UserValue,
library_error::LibraryError, remote_access_error::RemoteAccessError,
},
games::library::{get_current_meta, uninstall_game_logic},
AppState,
@ -19,16 +19,16 @@ use super::{
};
#[tauri::command]
pub fn fetch_library(app: AppHandle) -> UserValue<Vec<Game>, RemoteAccessError> {
fetch_library_logic(app).into()
pub fn fetch_library(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
fetch_library_logic(app)
}
#[tauri::command]
pub fn fetch_game(
game_id: String,
app: tauri::AppHandle,
) -> UserValue<FetchGameStruct, RemoteAccessError> {
fetch_game_logic(game_id, app).into()
) -> Result<FetchGameStruct, RemoteAccessError> {
fetch_game_logic(game_id, app)
}
#[tauri::command]
@ -37,21 +37,21 @@ pub fn fetch_game_status(id: String) -> GameStatusWithTransient {
}
#[tauri::command]
pub fn uninstall_game(game_id: String, app_handle: AppHandle) -> UserValue<(), LibraryError> {
pub fn uninstall_game(game_id: String, app_handle: AppHandle) -> Result<(), LibraryError> {
let meta = match get_current_meta(&game_id) {
Some(data) => data,
None => return UserValue::Err(LibraryError::MetaNotFound(game_id)),
None => return Err(LibraryError::MetaNotFound(game_id)),
};
println!("{:?}", meta);
uninstall_game_logic(meta, &app_handle);
UserValue::Ok(())
Ok(())
}
#[tauri::command]
pub fn fetch_game_verion_options(
game_id: String,
state: tauri::State<'_, Mutex<AppState>>,
) -> UserValue<Vec<GameVersionOption>, RemoteAccessError> {
fetch_game_verion_options_logic(game_id, state).into()
) -> Result<Vec<GameVersionOption>, RemoteAccessError> {
fetch_game_verion_options_logic(game_id, state)
}

View File

@ -1,8 +1,7 @@
use std::sync::{mpsc::SendError, Arc, Mutex};
use crate::{
download_manager::{download_manager::DownloadManagerSignal, downloadable::Downloadable},
error::user_error::UserValue,
download_manager::{download_manager::DownloadManagerSignal, downloadable::Downloadable, internal_error::InternalError},
AppState,
};
@ -14,7 +13,7 @@ pub fn download_game(
game_version: String,
install_dir: usize,
state: tauri::State<'_, Mutex<AppState>>,
) -> UserValue<(), SendError<DownloadManagerSignal>> {
) -> Result<(), InternalError<DownloadManagerSignal>> {
let sender = state.lock().unwrap().download_manager.get_sender();
let game_download_agent = Arc::new(Box::new(GameDownloadAgent::new(
game_id,
@ -22,10 +21,9 @@ pub fn download_game(
install_dir,
sender,
)) as Box<dyn Downloadable + Send + Sync>);
state
Ok(state
.lock()
.unwrap()
.download_manager
.queue_download(game_download_agent)
.into()
.queue_download(game_download_agent)?)
}

View File

@ -136,7 +136,7 @@ impl GameDownloadAgent {
let manifest_url = base_url
.join(
format!(
"/api/v1/client/metadata/manifest?id={}&version={}",
"/api/v1/client/game/manifest?id={}&version={}",
self.id,
encode(&self.version)
)

View File

@ -180,7 +180,7 @@ pub fn fetch_game_verion_options_logic(
) -> Result<Vec<GameVersionOption>, RemoteAccessError> {
let client = reqwest::blocking::Client::new();
let response = make_request(&client, &["/api/v1/client/metadata/versions"], &[("id", &game_id)], |r| {
let response = make_request(&client, &["/api/v1/client/game/versions"], &[("id", &game_id)], |r| {
r.header("Authorization", generate_authorization_header())
})?.send()?;