Skip to content

Commit

Permalink
feat: propagate log error inside command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
albugowy15 committed Dec 21, 2023
1 parent ce298a6 commit 2030d24
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 40 deletions.
8 changes: 7 additions & 1 deletion src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
44 changes: 29 additions & 15 deletions src/commands/compare.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::path::PathBuf;

use anyhow::{Context, Result};

use crate::{
commands::prepare_data,
db::{
Expand All @@ -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<ClassFromSchedule> = excel.get_schedule();

log::info!(
Expand Down Expand Up @@ -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(())
}
8 changes: 7 additions & 1 deletion src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
46 changes: 28 additions & 18 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::{Context, Result};

use crate::{
commands::prepare_data,
db::{
Expand All @@ -14,23 +12,34 @@ use crate::{
},
};

pub async fn update_handler(
push: &bool,
file: &PathBuf,
sheet: &String,
outdir: &Option<PathBuf>,
) -> 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<PathBuf>) {
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();
Expand Down Expand Up @@ -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(())
}
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod db;
mod utils;

use crate::utils::env;
use anyhow::Result;
use clap::Parser;
use commands::Commands;

Expand All @@ -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 {
Expand All @@ -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;
}
Expand All @@ -41,5 +42,4 @@ async fn main() -> Result<()> {
}

log::info!("Done");
Ok(())
}

0 comments on commit 2030d24

Please sign in to comment.