From 2030d2445562e9d7f6b1f1727af1f1b0793b0367 Mon Sep 17 00:00:00 2001 From: albugowy15 Date: Thu, 21 Dec 2023 14:47:38 +0700 Subject: [PATCH] feat: propagate log error inside command handler --- src/commands/clean.rs | 8 ++++++- src/commands/compare.rs | 44 +++++++++++++++++++++++++-------------- src/commands/sync.rs | 8 ++++++- src/commands/update.rs | 46 +++++++++++++++++++++++++---------------- src/main.rs | 10 ++++----- 5 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/commands/clean.rs b/src/commands/clean.rs index 460bdc1..e947c2a 100644 --- a/src/commands/clean.rs +++ b/src/commands/clean.rs @@ -4,7 +4,13 @@ use crate::db::{ }; pub async fn clean_handler() { - let pool = Connection::create_connection().await.unwrap(); + let pool = match Connection::create_connection().await { + Ok(pool) => pool, + Err(e) => { + log::error!("Failed to create a db connection: {}", e); + return; + } + }; log::info!("Clean up invalid foreign key"); let many_to_many_repo = ManyToManyRepository::new(&pool); diff --git a/src/commands/compare.rs b/src/commands/compare.rs index ff938cb..d462d65 100644 --- a/src/commands/compare.rs +++ b/src/commands/compare.rs @@ -1,7 +1,5 @@ use std::path::PathBuf; -use anyhow::{Context, Result}; - use crate::{ commands::prepare_data, db::{ @@ -17,25 +15,39 @@ use crate::{ }, }; -pub async fn compare_handler(file: &PathBuf, sheet: &str, outdir: &PathBuf) -> Result<()> { - let pool = Connection::create_connection().await?; +pub async fn compare_handler(file: &PathBuf, sheet: &str, outdir: &PathBuf) { + let pool = match Connection::create_connection().await { + Ok(pool) => pool, + Err(e) => { + log::error!("Failed to create a db connection: {}", e); + return; + } + }; log::info!("Get existing schedule from DB"); let class_repo = ClassRepository::new(&pool); let (mut db_classes_res, repo_data_res) = - tokio::try_join!(class_repo.get_schedule(), prepare_data(&pool)).map_err(|e| { - log::error!("Error getting schedule: {}", e); - e - })?; + match tokio::try_join!(class_repo.get_schedule(), prepare_data(&pool)) { + Ok((db_classes_res, repo_data_res)) => (db_classes_res, repo_data_res), + Err(e) => { + log::error!("Error getting schedule: {}", e); + return; + } + }; log::info!("Get latest schedule from Excel"); - let excel = Excel::new( + let excel = match Excel::new( file, sheet, repo_data_res.0, repo_data_res.1, repo_data_res.2, - ) - .with_context(|| "Error opening excel file")?; + ) { + Ok(excel) => excel, + Err(e) => { + log::error!("Error opening excel file: {}", e); + return; + } + }; let excel_classes: Vec = excel.get_schedule(); log::info!( @@ -63,13 +75,15 @@ pub async fn compare_handler(file: &PathBuf, sheet: &str, outdir: &PathBuf) -> R deleted.len() ); log::info!("Write the result to {:?}", &outdir); - OutWriter::new(outdir) + if let Err(e) = OutWriter::new(outdir) .await - .with_context(|| format!("Error creating {:?}", outdir))? + .unwrap() .write_compare_result(&added, &changed, &deleted) .await - .with_context(|| "Error writing result")?; + { + log::error!("Error writing result: {}", e); + return; + }; pool.close().await; - Ok(()) } diff --git a/src/commands/sync.rs b/src/commands/sync.rs index 86b0b32..4fcded5 100644 --- a/src/commands/sync.rs +++ b/src/commands/sync.rs @@ -4,7 +4,13 @@ use crate::db::{ }; pub async fn sync_handler() { - let pool = Connection::create_connection().await.unwrap(); + let pool = match Connection::create_connection().await { + Ok(pool) => pool, + Err(e) => { + log::error!("Failed to create a db connection: {}", e); + return; + } + }; log::info!("Sync taken from Class"); let class_repo = ClassRepository::new(&pool); diff --git a/src/commands/update.rs b/src/commands/update.rs index 647eb38..ee4fba0 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,7 +1,5 @@ use std::{path::PathBuf, sync::Arc}; -use anyhow::{Context, Result}; - use crate::{ commands::prepare_data, db::{ @@ -14,23 +12,34 @@ use crate::{ }, }; -pub async fn update_handler( - push: &bool, - file: &PathBuf, - sheet: &String, - outdir: &Option, -) -> Result<()> { - let pool = Connection::create_connection().await?; - let repo_data = prepare_data(&pool).await?; +pub async fn update_handler(push: &bool, file: &PathBuf, sheet: &String, outdir: &Option) { + let pool = match Connection::create_connection().await { + Ok(pool) => pool, + Err(e) => { + log::error!("Failed to create a db connection: {}", e); + return; + } + }; + let repo_data = match prepare_data(&pool).await { + Ok(repo_data) => repo_data, + Err(e) => { + log::error!("Failed prepare initial data: {}", e); + return; + } + }; log::info!("Parse class schedule from Excel"); - let excel = - Excel::new(file, sheet, repo_data.0, repo_data.1, repo_data.2).with_context(|| { - format!( - "Error opening {} with sheet name '{:?}'", + let excel = match Excel::new(file, sheet, repo_data.0, repo_data.1, repo_data.2) { + Ok(excel) => excel, + Err(e) => { + log::error!( + "Error opening {} with sheet name '{:?}': {}", &file.display(), &sheet, - ) - })?; + e + ); + return; + } + }; let list_class = Arc::new(excel.get_schedule()); let mut handles = Vec::new(); @@ -63,8 +72,9 @@ pub async fn update_handler( handles.push(handle); } for handle in handles { - handle.await?; + if let Err(e) = handle.await { + log::error!("Thread error: {}", e) + }; } pool.close().await; - Ok(()) } diff --git a/src/main.rs b/src/main.rs index 16a2cb9..c3748a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ mod db; mod utils; use crate::utils::env; -use anyhow::Result; use clap::Parser; use commands::Commands; @@ -15,9 +14,11 @@ struct Cli { } #[tokio::main] -async fn main() -> Result<()> { +async fn main() { + // setup env for logger and rust backtrace env::setup_env(); + // parse cli command and args from struct let cli = Cli::parse(); match &cli.command { @@ -26,12 +27,12 @@ async fn main() -> Result<()> { file, sheet, outdir, - } => commands::update::update_handler(push, file, sheet, outdir).await?, + } => commands::update::update_handler(push, file, sheet, outdir).await, Commands::Compare { file, sheet, outdir, - } => commands::compare::compare_handler(file, sheet, outdir).await?, + } => commands::compare::compare_handler(file, sheet, outdir).await, Commands::Clean => { commands::clean::clean_handler().await; } @@ -41,5 +42,4 @@ async fn main() -> Result<()> { } log::info!("Done"); - Ok(()) }